String Extensions

String manipulation is so common in any coding environment and JavaScript is no exception. These string extensions are designed to provide common generic value to the existing string object. W3Schools.com provides good complete documentation of the string object at http://www.w3schools.com/jsref/jsref_obj_string.asp

Method
String.prototype.trim()
Trim leading and trailing blanks from a string.
String.prototype.ltrim()
Trim leading blanks from a string.
String.prototype.rtrim()
Trim trailing blanks from a string.
String.prototype.contains(str)
Determine if the current string contains a specified string.
String.prototype.startsWith(str)
Determine if the current string starts with a specified string.
String.prototype.endsWith(str)
Determine if the current string ends with a specified string.
String.prototype.removePrefix(str)
If this string starts with the specified string, then remove the specified string from beginning of this string.
String.prototype.removeSuffix(str)
If this string ends with the specified string, then remove the specified string from end of this string.
String.prototype.beforeFirst(str, sDefault)
Obtain the string before the first occurance of a specified string.
String.prototype.afterFirst(str, sDefault)
Obtain the string after the first occurance of a specified string.
String.prototype.beforeLast(str, sDefault)
Obtain the string before the last occurance of a specified string.
String.prototype.afterLast(str, sDefault)
Obtain the string after the last occurance of a specified string.

function String.prototype.trim()

Trim leading and trailing blanks from a string.

Arguments:
(return)
String with leading and trailing blanks removed.

This method is so common in all string manipulation utilities, one wonders why it never was include in the original JavaScript string object. I am sure everyone has their own version, this is our implementation.

   "".trim()                        // ""
   " ".trim()                       // ""
   "this is a test".trim()          // "this is a test"
   " this is a test".trim()         // "this is a test"
   "this is a test ".trim()         // "this is a test"
   " this is a test ".trim()        // "this is a test"
      

function String.prototype.ltrim()

Trim leading blanks from a string.

Arguments:
(return)
String with leading blanks removed.
   "".ltrim()                       // ""
   " ".ltrim()                      // ""
   "this is a test".ltrim()         // "this is a test"
   " this is a test".ltrim()        // "this is a test"
   "this is a test ".ltrim()        // "this is a test "
   " this is a test ".ltrim()       // "this is a test "
      

function String.prototype.rtrim()

Trim trailing blanks from a string.

Arguments:
(return)
String with trailing blanks removed.
   "".rtrim()                       // ""
   " ".rtrim()                      // ""
   "this is a test".rtrim()         // "this is a test"
   " this is a test".rtrim()        // " this is a test"
   "this is a test ".rtrim()        // "this is a test"
   " this is a test ".rtrim()       // " this is a test"
      

function String.prototype.contains(str)

Determine if the current string contains a specified string.

Arguments:
str
String to check if exists in the current string.
(return)
"true" if the specified string exists in the current string. If "str" is null or not a string, then return "false".
   "".contains()                          // false
   "".contains(null)                      // false
   "".contains("")                        // true
   "".contains("is")                      // false
   "    ".contains("")                    // true
   "    ".contains("is")                  // false
   "abcd".contains()                      // false
   "abcd".contains(null)                  // false
   "abcd".contains("is")                  // false
   "abcd".contains("ab")                  // true
   "abcd".contains("cd")                  // true
   "abcd".contains("abcdefg")             // false
   "this is a test".contains("is")        // true
   "this is a test".contains(" is ")      // true
      

function String.prototype.startsWith(str)

Determine if the current string starts with a specified string.

Arguments:
str
String to check if the current string starts with this string.
(return)
"true" if the current string starts with the specified string. If "str" is null or not a string, then return "false".
   "".startsWith()                          // false
   "".startsWith(null)                      // false
   "".startsWith("")                        // true
   "".startsWith("is")                      // false
   "    ".startsWith("")                    // true
   "    ".startsWith("is")                  // false
   "    ".startsWith(" ")                   // true
   "abcd".startsWith()                      // false
   "abcd".startsWith(null)                  // false
   "abcd".startsWith("")                    // true
   "abcd".startsWith("is")                  // false
   "abcd".startsWith("ab")                  // true
   "abcd".startsWith("cd")                  // false
   "abcd".startsWith("abcdefg")             // false
   "this is a test".startsWith("is")        // false
   "this is a test".startsWith(" is ")      // false
   "this is a test".startsWith("this")      // true
   "this is a test".startsWith("test")      // false
      

function String.prototype.endsWith(str)

Determine if the current string ends with a specified string.

Arguments:
str
String to check if the current string ends with this string.
(return)
"true" if the current string ends with the specified string. If "str" is null or not a string, then return "false".
   "".endsWith()                            // false
   "".endsWith(null)                        // false
   "".endsWith("")                          // true
   "".endsWith("is")                        // false
   "    ".endsWith("")                      // true
   "    ".endsWith("is")                    // false
   "    ".endsWith(" ")                     // true
   "abcd".endsWith()                        // false
   "abcd".endsWith(null)                    // false
   "abcd".endsWith("")                      // true
   "abcd".endsWith("is")                    // false
   "abcd".endsWith("ab")                    // false
   "abcd".endsWith("cd")                    // true
   "abcd".endsWith("abcdefg")               // false
   "this is a test".endsWith("is")          // false
   "this is a test".endsWith(" is ")        // false
   "this is a test".endsWith("this")        // false
   "this is a test".endsWith("test")        // true
      

function String.prototype.removePrefix(str)

If this string starts with the specified string, then remove the specified string from beginning of this string.

Arguments:
str
String to remove from the beginning of this string.
(return)
Resulting string with string removed from the beginning as required.
   "".removePrefix()                          // ""
   "".removePrefix(null)                      // ""
   "".removePrefix("")                        // ""
   "".removePrefix("is")                      // ""
   "    ".removePrefix("")                    // "    "
   "    ".removePrefix("is")                  // "    "
   "    ".removePrefix(" ")                   // "   "
   "abcd".removePrefix()                      // "abcd"
   "abcd".removePrefix(null)                  // "abcd"
   "abcd".removePrefix("")                    // "abcd"
   "abcd".removePrefix("is")                  // "abcd"
   "abcd".removePrefix("ab")                  // "cd"
   "abcd".removePrefix("cd")                  // "abcd"
   "abcd".removePrefix("abcd")                // ""
   "abcd".removePrefix("abcdefg")             // "abcd"
   "this is a test".removePrefix("is")        // "this is a test"
   "this is a test".removePrefix(" is ")      // "this is a test"
   "this is a test".removePrefix("this")      // " is a test"
   "this is a test".removePrefix("test")      // "this is a test"
      

function String.prototype.removeSuffix(str)

If this string ends with the specified string, then remove the specified string from ending of this string.

Arguments:
str
String to remove from the end of this string.
(return)
Resulting string with string removed from the end as required.
   "".removeSuffix()                          // ""
   "".removeSuffix(null)                      // ""
   "".removeSuffix("")                        // ""
   "".removeSuffix("is")                      // ""
   "    ".removeSuffix("")                    // "    "
   "    ".removeSuffix("is")                  // "    "
   "    ".removeSuffix(" ")                   // "   "
   "abcd".removeSuffix()                      // "abcd"
   "abcd".removeSuffix(null)                  // "abcd"
   "abcd".removeSuffix("")                    // "abcd"
   "abcd".removeSuffix("is")                  // "abcd"
   "abcd".removeSuffix("ab")                  // "abcd"
   "abcd".removeSuffix("cd")                  // "ab"
   "abcd".removeSuffix("abcd")                // ""
   "abcd".removeSuffix("abcdefg")             // "abcd"
   "this is a test".removeSuffix("is")        // "this is a test"
   "this is a test".removeSuffix(" is ")      // "this is a test"
   "this is a test".removeSuffix("this")      // "this is a test"
   "this is a test".removeSuffix("test")      // "this is a "
      

function String.prototype.beforeFirst(str, sDefault)

Obtain the string before the first occurance of a specified string.

Arguments:
str
String to search for in this string.
sDefault
Default value if string not found. Default is this string.
(return)
The string before the first occurrence of specified string in "str" if found, otherwise the default value.

These procedures are designed to extract text base on a delimiter character that are more efficient and less cumbersome to use than the “indexOf” and “split” methods. The set of methods that have the prefix “begin” or “after” are designed to provide quick simple easy access to delimited text within a string.

For example to extract the domain and user name of a domain account name can be easily done with the following two method calls:

         var domainName = accountName .beforeFirst("\\", null);
         var userName = accountName.afterFirst("\\", accountName);
         

   "".beforeFirst()                          // ""
   "".beforeFirst(null)                      // ""
   "".beforeFirst("")                        // ""
   "".beforeFirst("is")                      // ""
   "    ".beforeFirst("")                    // ""
   "    ".beforeFirst("is")                  // "    "
   "    ".beforeFirst(" ")                   // ""
   "abcd".beforeFirst()                      // "abcd"
   "abcd".beforeFirst(null)                  // "abcd"
   "abcd".beforeFirst("")                    // ""
   "abcd".beforeFirst("is")                  // "abcd"
   "abcd".beforeFirst("ab")                  // ""
   "abcd".beforeFirst("cd")                  // "ab"
   "abcd".beforeFirst("abcd")                // ""
   "abcd".beforeFirst("abcdefg")             // "abcd"
   "this is a test".beforeFirst("is")        // "th"
   "this is a test".beforeFirst(" ")         // "this"
   "this is a test".beforeFirst(" is ")      // "this"
   "this is a test".beforeFirst("this")      // ""
   "this is a test".beforeFirst("test")      // "this is a "
   "this is a test".beforeFirst("xxxx")      // "this is a test"

   "".beforeFirst(null, null)                         // null
   "".beforeFirst(null, "default")                    // "default"
   "".beforeFirst("", "default")                      // ""
   "".beforeFirst("is", null)                         // null
   "".beforeFirst("is", "default")                    // "default"
   "    ".beforeFirst("", "default")                  // ""
   "    ".beforeFirst("is", "default")                // "default"
   "    ".beforeFirst(" ", "default")                 // ""
   "abcd".beforeFirst(null, "default")                // "default"
   "abcd".beforeFirst("", "default")                  // ""
   "abcd".beforeFirst("is", "default")                // "default"
   "abcd".beforeFirst("ab", "default")                // ""
   "abcd".beforeFirst("cd", "default")                // "ab"
   "abcd".beforeFirst("abcd", "default")              // ""
   "abcd".beforeFirst("abcdefg", "default")           // "default"
   "this is a test".beforeFirst("is", "default")      // "th"
   "this is a test".beforeFirst(" ", "default")       // "this"
   "this is a test".beforeFirst(" is ", "default")    // "this"
   "this is a test".beforeFirst("this", "default")    // ""
   "this is a test".beforeFirst("test", "default")    // "this is a "
   "this is a test".beforeFirst("xxxx", "default")    // "default"
      

function String.prototype.afterFirst(str, sDefault)

Obtain the string after the first occurance of a specified string.

Arguments:
str
String to search for in this string.
sDefault
Default value if string not found. Default is this string.
(return)
The string after the first occurrence of specified string in "str" if found, otherwise the default value.

These procedures are designed to extract text base on a delimiter character that are more efficient and less cumbersome to use than the “indexOf” and “split” methods. The set of methods that have the prefix “begin” or “after” are designed to provide quick simple easy access to delimited text within a string.

For example to extract the domain and user name of a domain account name can be easily done with the following two method calls:

         var domainName = accountName .beforeLast("\\", null);
         var userName = accountName.afterFirst("\\", accountName);
         

   "".afterFirst()                          // ""
   "".afterFirst(null)                      // ""
   "".afterFirst("")                        // ""
   "".afterFirst("is")                      // ""
   "    ".afterFirst("")                    // "    "
   "    ".afterFirst("is")                  // "    "
   "    ".afterFirst(" ")                   // "   "
   "abcd".afterFirst()                      // "abcd"
   "abcd".afterFirst(null)                  // "abcd"
   "abcd".afterFirst("")                    // "abcd"
   "abcd".afterFirst("is")                  // "abcd"
   "abcd".afterFirst("ab")                  // "cd"
   "abcd".afterFirst("cd")                  // ""
   "abcd".afterFirst("abcd")                // ""
   "abcd".afterFirst("abcdefg")             // "abcd"
   "this is a test".afterFirst("is")        // " is a test"
   "this is a test".afterFirst(" ")         // "is a test"
   "this is a test".afterFirst(" is ")      // "a test"
   "this is a test".afterFirst("this")      // " is a test"
   "this is a test".afterFirst("test")      // ""
   "this is a test".afterFirst("xxxx")      // "this is a test"

   "".afterFirst(null, null)                         // null
   "".afterFirst(null, "default")                    // "default"
   "".afterFirst("", "default")                      // ""
   "".afterFirst("is", null)                         // null
   "".afterFirst("is", "default")                    // "default"
   "    ".afterFirst("", "default")                  // "    "
   "    ".afterFirst("is", "default")                // "default"
   "    ".afterFirst(" ", "default")                 // "   "
   "abcd".afterFirst(null, "default")                // "default"
   "abcd".afterFirst("", "default")                  // "abcd"
   "abcd".afterFirst("is", "default")                // "default"
   "abcd".afterFirst("ab", "default")                // "cd"
   "abcd".afterFirst("cd", "default")                // ""
   "abcd".afterFirst("abcd", "default")              // ""
   "abcd".afterFirst("abcdefg", "default")           // "default"
   "this is a test".afterFirst("is", "default")      // " is a test"
   "this is a test".afterFirst(" ", "default")       // "is a test"
   "this is a test".afterFirst(" is ", "default")    // "a test"
   "this is a test".afterFirst("this", "default")    // " is a test"
   "this is a test".afterFirst("test", "default")    // ""
   "this is a test".afterFirst("xxxx", "default")    // "default"
      

function String.prototype.beforeLast(str, sDefault)

Obtain the string before the last occurance of a specified string.

Arguments:
str
String to search for in this string.
sDefault
Default value if string not found. Default is this string.
(return)
The string before the last occurrence of specified string in "str" if found, otherwise the default value.

These procedures are designed to extract text base on a delimiter character that are more efficient and less cumbersome to use than the “indexOf” and “split” methods. The set of methods that have the prefix “begin” or “after” are designed to provide quick simple easy access to delimited text within a string.

For example to extract the domain and user name of a domain account name can be easily done with the following two method calls:

         var domainName = accountName .beforeLast("\\", null);
         var userName = accountName.afterFirst("\\", accountName);
         

   "".beforeLast()                          // ""
   "".beforeLast(null)                      // ""
   "".beforeLast("")                        // ""
   "".beforeLast("is")                      // ""
   "    ".beforeLast("")                    // "    "
   "    ".beforeLast("is")                  // "    "
   "    ".beforeLast(" ")                   // "   "
   "abcd".beforeLast()                      // "abcd"
   "abcd".beforeLast(null)                  // "abcd"
   "abcd".beforeLast("")                    // "abcd"
   "abcd".beforeLast("is")                  // "abcd"
   "abcd".beforeLast("ab")                  // ""
   "abcd".beforeLast("cd")                  // "ab"
   "abcd".beforeLast("abcd")                // ""
   "abcd".beforeLast("abcdefg")             // "abcd"
   "this is a test".beforeLast("is")        // "this "
   "this is a test".beforeLast(" ")         // "this is a"
   "this is a test".beforeLast(" is ")      // "this"
   "this is a test".beforeLast("this")      // ""
   "this is a test".beforeLast("test")      // "this is a "
   "this is a test".beforeLast("xxxx")      // "this is a test"

   "".beforeLast(null, null)                         // null
   "".beforeLast(null, "default")                    // "default"
   "".beforeLast("", "default")                      // ""
   "".beforeLast("is", null)                         // null
   "".beforeLast("is", "default")                    // "default"
   "    ".beforeLast("", "default")                  // "    "
   "    ".beforeLast("is", "default")                // "default"
   "    ".beforeLast(" ", "default")                 // "   "
   "abcd".beforeLast(null, "default")                // "default"
   "abcd".beforeLast("", "default")                  // "abcd"
   "abcd".beforeLast("is", "default")                // "default"
   "abcd".beforeLast("ab", "default")                // ""
   "abcd".beforeLast("cd", "default")                // "ab"
   "abcd".beforeLast("abcd", "default")              // ""
   "abcd".beforeLast("abcdefg", "default")           // "default"
   "this is a test".beforeLast("is", "default")      // "this "
   "this is a test".beforeLast(" ", "default")       // "this is a"
   "this is a test".beforeLast(" is ", "default")    // "this"
   "this is a test".beforeLast("this", "default")    // ""
   "this is a test".beforeLast("test", "default")    // "this is a "
   "this is a test".beforeLast("xxxx", "default")    // "default"
      

function String.prototype.afterLast(str, sDefault)

Obtain the string after the last occurance of a specified string.

Arguments:
str
String to search for in this string.
sDefault
Default value if string not found. Default is this string.
(return)
The string after the last occurrence of specified string in "str" if found, otherwise the default value.

These procedures are designed to extract text base on a delimiter character that are more efficient and less cumbersome to use than the “indexOf” and “split” methods. The set of methods that have the prefix “begin” or “after” are designed to provide quick simple easy access to delimited text within a string.

For example to extract the domain and user name of a domain account name can be easily done with the following two method calls:

         var domainName = accountName .beforeLast("\\", null);
         var userName = accountName.afterFirst("\\", accountName);
         

   "".afterLast()                          // ""
   "".afterLast(null)                      // ""
   "".afterLast("")                        // ""
   "".afterLast("is")                      // ""
   "    ".afterLast("")                    // ""
   "    ".afterLast("is")                  // "    "
   "    ".afterLast(" ")                   // ""
   "abcd".afterLast()                      // "abcd"
   "abcd".afterLast(null)                  // "abcd"
   "abcd".afterLast("")                    // ""
   "abcd".afterLast("is")                  // "abcd"
   "abcd".afterLast("ab")                  // "cd"
   "abcd".afterLast("cd")                  // ""
   "abcd".afterLast("abcd")                // ""
   "abcd".afterLast("abcdefg")             // "abcd"
   "this is a test".afterLast("is")        // " a test"
   "this is a test".afterLast(" ")         // "test"
   "this is a test".afterLast(" is ")      // "a test"
   "this is a test".afterLast("this")      // " is a test"
   "this is a test".afterLast("test")      // ""
   "this is a test".afterLast("xxxx")      // "this is a test"

   "".afterLast(null, null)                         // null
   "".afterLast(null, "default")                    // "default"
   "".afterLast("", "default")                      // ""
   "".afterLast("is", null)                         // null
   "".afterLast("is", "default")                    // "default"
   "    ".afterLast("", "default")                  // ""
   "    ".afterLast("is", "default")                // "default"
   "    ".afterLast(" ", "default")                 // ""
   "abcd".afterLast(null, "default")                // "default"
   "abcd".afterLast("", "default")                  // ""
   "abcd".afterLast("is", "default")                // "default"
   "abcd".afterLast("ab", "default")                // "cd"
   "abcd".afterLast("cd", "default")                // ""
   "abcd".afterLast("abcd", "default")              // ""
   "abcd".afterLast("abcdefg", "default")           // "default"
   "this is a test".afterLast("is", "default")      // " a test"
   "this is a test".afterLast(" ", "default")       // " test"
   "this is a test".afterLast(" is ", "default")    // "a test"
   "this is a test".afterLast("this", "default")    // " is a test"
   "this is a test".afterLast("test", "default")    // ""
   "this is a test".afterLast("xxxx", "default")    // "default"