MODULES Uize.String
SEARCHEXAMPLESVIEW SOURCE CODE

1. Introduction

The Uize.String module eases working with strings, and supports trimming, camel-casing, multi-line indenting, starts-with / ends-with tests, and more.

DEVELOPERS: Chris van Rensburg

The Uize.String module is a package under the Uize namespace.

2. Static Methods

2.1. Uize.String.contains

Returns a boolean, indicating whether or not the specified source string contains the specified substring.

SYNTAX

containsBOOL = Uize.String.contains (sourceSTR,subSTR);

If a source string starts with or ends with a substring, then that source string also contains the substring. In other words, if Uize.String.startsWith (sourceStr,subStr) returns true, or if Uize.String.endsWith (sourceStr,subStr) returns true, then Uize.String.contains (sourceStr,subStr) must also return true.

EXAMPLES

Uize.String.contains ('JavaScript','Java');                 // returns true
Uize.String.contains ('JavaScript','JavaScript');           // returns true
Uize.String.contains ('JavaScript','Script');               // returns true
Uize.String.contains ('JavaScript','S');                    // returns true
Uize.String.contains ('JavaScript','ava');                  // returns true
Uize.String.contains ('JavaScript','');                     // returns true
Uize.String.contains ('','');                               // returns true
Uize.String.contains ('JavaScript','JAVASCRIPT');           // returns false
Uize.String.contains ('JavaScript','script');               // returns false
Uize.String.contains ('Java','JavaScript');                 // returns false
Uize.String.contains ('JavaScript','Java Script');          // returns false
Uize.String.contains ('JavaScript','JavaScript   ');        // returns false
Uize.String.contains ('JavaScript','   JavaScript');        // returns false
Uize.String.contains ('JavaScript','JavaScript Framework'); // returns false

NOTES

see the related Uize.String.startsWith and Uize.String.endsWith static methods
this method is case sensitive

2.2. Uize.String.endsWith

Returns a boolean, indicating whether or not the specified source string ends with the specified substring.

SYNTAX

endsWithBOOL = Uize.String.endsWith (sourceSTR,subSTR);

The test that this method performs is case and space sensitive. In cases where you need to test without regards to case or whitespace, it is best to construct a regular expression using the "$" (anchor to end) metacharacter and the i (case-insensitivity) switch.

EXAMPLES

Uize.String.endsWith ('JavaScript','Java');                 // returns false
Uize.String.endsWith ('Java','JavaScript');                 // returns false
Uize.String.endsWith ('JavaScript','JavaScript');           // returns true
Uize.String.endsWith ('JavaScript','Java Script');          // returns false
Uize.String.endsWith ('JavaScript','JavaScript   ');        // returns false
Uize.String.endsWith ('JavaScript','   JavaScript');        // returns false
Uize.String.endsWith ('JavaScript','JAVASCRIPT');           // returns false
Uize.String.endsWith ('JavaScript','Script');               // returns true
Uize.String.endsWith ('JavaScript','JavaScript Framework'); // returns false
Uize.String.endsWith ('JavaScript','');                     // returns true

NOTES

see the companion Uize.String.startsWith static method
see the related Uize.String.contains static method
when the value '' (empty string) is specified for the subSTR parameter, this method will return true (all strings can be said to end with an empty string)

2.3. Uize.String.hasPadding

Returns a boolean, indicidating whether or not the specified string has whitespace padding on either - or both - of its sides (ie. leading or trailing whitespace).

SYNTAX

hasPaddingBOOL = Uize.String.hasPadding (sourceSTR);

EXAMPLES

Uize.String.hasPadding ('   leading whitespace');                  // returns true
Uize.String.hasPadding ('trailing whitespace     ');               // returns true
Uize.String.hasPadding ('   leading and trailing whitespace   ');  // returns true
Uize.String.hasPadding ('no         padding');                     // returns false
Uize.String.hasPadding ('   ');                                    // returns true
Uize.String.hasPadding ('');                                       // returns false

NOTES

see the related Uize.String.trim static method

2.4. Uize.String.hugJoin

Returns a string, that is the concatenation of the specified array of items, where a prefix and suffix can be specified for hugging each item in the array, and where an optional separator can additionally be specified.

SYNTAX

joinedSTR = Uize.String.hugJoin (itemsARRAY,itemPrefixSTR,itemSuffixSTR);

EXAMPLE 1

var actions = ['view','reset','save','open','close'];
alert (Uize.String.hugJoin (actions,'[ ',' ]'));

EXAMPLE 1 - OUTPUT

[ view ][ reset ][ save ][ open ][ close ]

VARIATION

joinedSTR = Uize.String.hugJoin (itemsARRAY,itemPrefixSTR,itemSuffixSTR,separatorSTR);

When the optional separatorSTR parameter is specified, then the items being joined will be separated by the specified separator string. This provides you with the functionality you would normally get from the built-in join instance method of the Array object.

Technically, the statement Uize.String.hugJoin (array,'','',separator) would be equivalent to the statement array.join (separator). But, if you just wanted to join an array with a separator string, then you would just use the join method, so the separatorSTR parameter is the last parameter and is optional for the Uize.String.hugJoin method, since the assumption is that you're likely using this method for its prefix/suffix feature.

EXAMPLE 2

var actions = ['view','reset','save','open','close'];
alert (Uize.String.hugJoin (actions,'[ ',' ]',' - '));

EXAMPLE 2 - OUTPUT

[ view ] - [ reset ] - [ save ] - [ open ] - [ close ]

EXAMPLE 3

var actions = ['view','reset','save','open','close'];
alert (Uize.String.hugJoin (actions,'\t','\n')); // on separate lines, indented

EXAMPLE 3 - OUTPUT

  view
  reset
  save
  open
  close

EXAMPLE 4

var actions = ['view','reset','save','open','close'];
alert (Uize.String.hugJoin (actions,'action: "','"\n','---------------\n'));

EXAMPLE 4 - OUTPUT

action: "view"
---------------
action: "reset"
---------------
action: "save"
---------------
action: "open"
---------------
action: "close"

2.5. Uize.String.joinUsingSuffixPriority

Returns a string, that is the concatenation of the two specified strings, limited to the specified maximum length, and such that the suffix string takes precedence if any characters must be lost in order to limit the length of the resulting string.

SYNTAX

joinedSTR = Uize.String.joinUsingSuffixPriority (prefixSTR,suffixSTR,maxLengthINT);

EXAMPLE

Uize.String.joinUsingSuffixPriority ('Some Greate Product Title',' - Customized',30);

In the above example, this method would produce the result 'Some Greate Produ - Customized'.

2.6. Uize.String.limitLength

Returns a string, that is the specified source string limited to the specified maximum length.

SYNTAX

limitedSTR = Uize.String.limitLength (sourceSTR,maxLengthINT);

If the string specified in the sourceSTR has to be truncated, it will be truncated to accommodate an ellipsis of "..." (three periods), such that the final length of the returned string is guaranteed to be no greater than the maximum length specified in the maxLengthINT parameter.

EXAMPLE

Uize.String.limitLength ('012345678901',15);        // returns '012345678901'
Uize.String.limitLength ('0123456789012',15);       // returns '0123456789012'
Uize.String.limitLength ('01234567890123',15);      // returns '01234567890123'
Uize.String.limitLength ('012345678901234',15);     // returns '012345678901234'
Uize.String.limitLength ('0123456789012345',15);    // returns '012345678901...'
Uize.String.limitLength ('01234567890123456',15);   // returns '012345678901...'
Uize.String.limitLength ('012345678901234567',15);  // returns '012345678901...'

Notice how, once the limit of 15 characters has been hit, all the resulting strings are only 15 characters long, with the last three characters being the ellipsis periods.

2.7. Uize.String.repeat

Returns a string, that is the specified source string repeated the specified number of times.

SYNTAX

repeatedSTR = Uize.String.repeat (sourceSTR,repeatTimesINT);

EXAMPLE 1

var hundredDashes = Uize.String.repeat ('-',100);

EXAMPLE 2

var paddingStr = Uize.String.repeat (' ',paddingAmount);

EXAMPLE 3

var tenBrTags = Uize.String.repeat ('<br/>',10);

The value of the sourceSTR parameter can contain any number of any characters. In the above example, a string containing ten br tags is being generated.

2.8. Uize.String.splitInTwo

Returns an array of exactly two elements, representing the two segments of the specified source string after splitting it using the specified splitter string.

SYNTAX

twoPartsARRAY = Uize.String.splitInTwo (sourceSTR,splitterSTR);

EXAMPLE

var nameValue = Uize.String.splitInTwo ('TITLE: The Matrix: Reloaded',': ');

In the above example, the nameValue variable would be left with the array value ['TITLE','The Matrix: Reloaded']. In contrast, the built-in split method of the String object would produce the three element array ['TITLE','The Matrix','Reloaded'] when splitting the above string using ': '.

If the splitter string is not found within the source string, then the returned array will contain the entire source string for its first element, and an empty string for its second element.

EXAMPLE

var nameValue = Uize.String.splitInTwo ('TITLE',': ');

In the above example, the nameValue variable would be left with the array value ['TITLE',''].

VARIATION

twoPartsARRAY = Uize.String.splitInTwo (sourceSTR,splitterREGEXP);

When a splitterREGEXP parameter is specified, the sourceSTR value will be split on the regular expression, and the two resulting parts will exclude the substring that was matched by the splitter regular expression.

EXAMPLE

var nameValue =
  Uize.String.splitInTwo ('TITLE   :   The Matrix: Reloaded',new RegExp ('\\s*:\\s*')
;

In the above example, the nameValue variable would be left with the array value ['TITLE','The Matrix: Reloaded']. In this case, the regular expression specified for the splitterREGEXP parameter matches a substring of any number of spaces, followed by a colon, followed by any number of spaces - in other words, a colon with optional padding. The two parts of the result will not include the whitespace padding around the colon, since it was part of the splitter match.

2.9. Uize.String.startsWith

Returns a boolean, indicating whether or not the specified source string starts with the specified substring.

SYNTAX

startsWithBOOL = Uize.String.startsWith (sourceSTR,subSTR);

The test that this method performs is case and space sensitive. In cases where you need to test without regards to case or whitespace, it is best to construct a regular expression using the "^" (anchor to beginning) metacharacter and the i (case-insensitivity) switch.

EXAMPLES

Uize.String.startsWith ('JavaScript','Java');                 // returns true
Uize.String.startsWith ('Java','JavaScript');                 // returns false
Uize.String.startsWith ('JavaScript','JavaScript');           // returns true
Uize.String.startsWith ('JavaScript','Java Script');          // returns false
Uize.String.startsWith ('JavaScript','JavaScript   ');        // returns false
Uize.String.startsWith ('JavaScript','   JavaScript');        // returns false
Uize.String.startsWith ('JavaScript','JAVASCRIPT');           // returns false
Uize.String.startsWith ('JavaScript','Script');               // returns false
Uize.String.startsWith ('JavaScript','JavaScript Framework'); // returns false
Uize.String.startsWith ('JavaScript','');                     // returns true

NOTES

see the companion Uize.String.endsWith static method
see the related Uize.String.contains static method
when the value '' (empty string) is specified for the subSTR parameter, this method will return true (all strings can be said to start with an empty string)

2.10. Uize.String.toCamel

Returns a string, that is the specified source string converted to a camelCase formatted string.

SYNTAX

camelCaseSTR = Uize.String.toCamel (sourceSTR);

This method removes all non-word characters separating words in the source string, capitalizes the first letters of the words, and lowercases all other letters.

EXAMPLES

Uize.String.toCamel ('encode HTML entity');    // returns 'encodeHtmlEntity'
Uize.String.toCamel ('XML document');          // returns 'xmlDocument'
Uize.String.toCamel ('XML document',true);     // returns 'XmlDocument'
Uize.String.toCamel ('city, state, zip');      // returns 'cityStateZip'
Uize.String.toCamel ('www.uize.com');          // returns 'wwwUizeCom'
Uize.String.toCamel ('theme/css/button.css');  // returns 'themeCssButtonCss'
Uize.String.toCamel ('nav-arrow-horz-next');   // returns 'navArrowHorzNext'
Uize.String.toCamel ('json 2 XML');            // returns 'json2Xml'
Uize.String.toCamel ('--hyphens-are-cool--');  // returns 'hyphensAreCool'

The above example illustrates how the method will behave with a variety of input values.

VARIATION

camelCaseSTR = Uize.String.toCamel (sourceSTR,capFirstCharBOOL);

By default, the first letter of the camelCased string is lowercase, although the optional capFirstCharBOOL parameter allows control over this behavior. Specify the value true for this parameter and the first letter of the camelCased string will be uppercase.

VARIATION

camelCaseSTR = Uize.String.toCamel (stringSegmentsARRAY);

In addition to being able to camelCase a source string, the Uize.String.toCamel method can also generate a camelCase string from an array of string segments.

EXAMPLE

Uize.String.toCamel (['city','state','zip']);  // returns 'cityStateZip'

VARIATION

camelCaseSTR = Uize.String.toCamel (stringSegmentsARRAY,capFirstCharBOOL);

Naturally, the optional capFirstCharBOOL parameter can also be used when the stringSegmentsARRAY parameter is specified.

2.11. Uize.String.trim

Returns a string, that is the specified source string minus any whitespace padding to the left and right of the first and last non-whitespace characters, respectively.

SYNTAX

trimmedSTR = Uize.String.trim (sourceSTR);

EXAMPLES

Uize.String.trim ('  THIS IS A STRING  ');      // returns 'THIS IS A STRING'
Uize.String.trim ('\tTHIS IS A STRING\t');      // returns 'THIS IS A STRING'
Uize.String.trim ('\n\nTHIS IS A STRING\n\n');  // returns 'THIS IS A STRING'
Uize.String.trim ('  \t \n\n \t');              // returns ''

2.11.1. Working with Multi-line Strings

This method regards linebreak characters as whitespace.

Therefore, this method cannot be used to trim whitespace padding on a line by line basis. To trim line by line, use the Uize.String.Lines.trim method implemented in the Uize.String.Lines module that is dedicated to working with multi-line strings.

NOTES

see the companion Uize.String.trimLeft and Uize.String.trimRight static methods
see the related Uize.String.hasPadding static method

2.12. Uize.String.trimLeft

Returns a string, that is the specified source string minus any whitespace padding to the left of the first non-whitespace character or the end of the string (ie. leading whitespace).

SYNTAX

leftTrimmedSTR = Uize.String.trimLeft (sourceSTR);

EXAMPLES

Uize.String.trim ('  THIS IS A STRING  ');      // returns 'THIS IS A STRING  '
Uize.String.trim ('\tTHIS IS A STRING\t');      // returns 'THIS IS A STRING\t'
Uize.String.trim ('\n\nTHIS IS A STRING\n\n');  // returns 'THIS IS A STRING\n\n'
Uize.String.trim ('  \t \n\n \t');              // returns ''

2.12.1. Working with Multi-line Strings

This method regards linebreak characters as whitespace.

Therefore, this method cannot be used to trim whitespace padding on a line by line basis. To left trim line by line, use the Uize.String.Lines.trimLeft method implemented in the Uize.String.Lines module that is dedicated to working with multi-line strings.

NOTES

see the companion Uize.String.trim and Uize.String.trimRight static methods

2.13. Uize.String.trimRight

Returns a string, that is the specified source string minus any whitespace padding to the right of the last non-whitespace character or the start of the string (ie. trailing whitespace).

SYNTAX

rightTrimmedSTR = Uize.String.trimRight (sourceSTR);

EXAMPLES

Uize.String.trim ('  THIS IS A STRING  ');      // returns '  THIS IS A STRING'
Uize.String.trim ('\tTHIS IS A STRING\t');      // returns '\tTHIS IS A STRING'
Uize.String.trim ('\n\nTHIS IS A STRING\n\n');  // returns '\n\nTHIS IS A STRING'
Uize.String.trim ('  \t \n\n \t');              // returns ''

2.13.1. Working with Multi-line Strings

This method regards linebreak characters as whitespace.

Therefore, this method cannot be used to trim whitespace padding on a line by line basis. To right trim line by line, use the Uize.String.Lines.trimRight method implemented in the Uize.String.Lines module that is dedicated to working with multi-line strings.

NOTES

see the companion Uize.String.trim and Uize.String.trimLeft static methods