diff options
Diffstat (limited to 'js/string.js')
| -rw-r--r-- | js/string.js | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/js/string.js b/js/string.js new file mode 100644 index 0000000..fa25527 --- /dev/null +++ b/js/string.js @@ -0,0 +1,24 @@ +/**
+ * Strips all leading and ending whitespaces.
+ * @returns the trimmed string.
+ */
+function trim() {
+ var str = this.replace(/^\s+/, "");
+ var ws = /\s/;
+ var i = str.length;
+ while (ws.test(str.charAt(--i)));
+ return str.slice(0, i + 1);
+}
+
+/**
+ * Checks whether a string is empty or not.
+ * The string is trimmed before checking if the length is 0.
+ * @return <code>true</code> if the trimmed string has a length of 0.
+ */
+function isEmpty() {
+ var testString = this.trim();
+ return 0 == testString.length;
+}
+
+String.prototype.trim = trim;
+String.prototype.isEmpty = isEmpty;
\ No newline at end of file |
