goog.string
Functions
canonicalizeNewlines( str ) → string
string
capitalize( str ) → string
string
Capitalizes a string, i.e. converts the first letter to uppercase and all other letters to lowercase, e.g.:
goog.string.capitalize('one') => 'One' goog.string.capitalize('ONE') => 'One' goog.string.capitalize('one two') => 'One two'
Note that this function does not trim initial whitespace.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
caseInsensitiveCompare( str1, str2 ) → number
number
caseInsensitiveContains( str, subString ) → boolean
boolean
caseInsensitiveEndsWith( str, suffix ) → boolean
boolean
caseInsensitiveEquals( str1, str2 ) → boolean
boolean
caseInsensitiveStartsWith( str, prefix ) → boolean
boolean
collapseBreakingSpaces( str ) → string
string
Removes the breaking spaces from the left and right of the string and collapses the sequences of breaking spaces in the middle into single spaces. The original and the result strings render the same way in HTML.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
collapseWhitespace( str ) → string
string
compareVersions( version1, version2 ) → number
number
contains( str, subString ) → boolean
boolean
countOf( s, ss ) → number
number
createUniqueString() → string
string
Generates and returns a string which is unique in the current document. This is useful, for example, to create unique IDs for DOM elements.
Parameters | None. | ||
---|---|---|---|
Returns |
|
editDistance( a, b ) → number
number
endsWith( str, suffix ) → boolean
boolean
escapeChar( c ) → string
string
escapeString( str ) → string
string
floatAwareCompare( str1, str2 ) → number
number
String comparison function that handles non-negative integer and fractional
numbers in a way humans might expect. Using this function, the string
'File 2.jpg' sorts before 'File 10.jpg', and '3.14' before '3.2'. Equivalent
to goog.string.intAwareCompare
apart from the way how it interprets
dots.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
getRandomString() → string
string
Returns a string with at least 64-bits of randomness.
Doesn't trust JavaScript's random function entirely. Uses a combination of random and current timestamp, and then encodes the string in base-36 to make it shorter.
Parameters | None. | ||
---|---|---|---|
Returns |
|
hashCode( str ) → number
number
String hash function similar to java.lang.String.hashCode(). The hash code for a string is computed as s[0] * 31 ^ (n - 1) + s[1] * 31 ^ (n - 2) + ... + s[n - 1], where s[i] is the ith character of the string and n is the length of the string. We mod the result to make it between 0 (inclusive) and 2^32 (exclusive).
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
htmlEscape( str, opt_isLikelyToContainHtmlChars ) → string
string
Escapes double quote '"' and single quote ''' characters in addition to '&', '<', and '>' so that a string can be included in an HTML tag attribute value within double or single quotes.
It should be noted that > doesn't need to be escaped for the HTML or XML to be valid, but it has been decided to escape it for consistency with other implementations.
With goog.string.DETECT_DOUBLE_ESCAPING, this function escapes also the lowercase letter "e".
NOTE(user): HtmlEscape is often called during the generation of large blocks of HTML. Using statics for the regular expressions and strings is an optimization that can more than half the amount of time IE spends in this function for large apps, since strings and regexes both contribute to GC allocations.
Testing for the presence of a character before escaping increases the number of function calls, but actually provides a speed increase for the average case -- since the average case often doesn't require the escaping of all 4 characters and indexOf() is much cheaper than replace(). The worst case does suffer slightly from the additional calls, therefore the opt_isLikelyToContainHtmlChars option has been included for situations where all 4 HTML entities are very likely to be present and need escaping.
Some benchmarks (times tended to fluctuate +-0.05ms): FireFox IE6 (no chars / average (mix of cases) / all 4 chars) no checks 0.13 / 0.22 / 0.22 0.23 / 0.53 / 0.80 indexOf 0.08 / 0.17 / 0.26 0.22 / 0.54 / 0.84 indexOf + re test 0.07 / 0.17 / 0.28 0.19 / 0.50 / 0.85
An additional advantage of checking if replace actually needs to be called is a reduction in the number of object allocations, so as the size of the application grows the difference between the various methods would increase.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
intAwareCompare( str1, str2 ) → number
number
String comparison function that handles non-negative integer numbers in a way humans might expect. Using this function, the string 'File 2.jpg' sorts before 'File 10.jpg', and 'Version 1.9' before 'Version 1.10'. The comparison is mostly case-insensitive, though strings that are identical except for case are sorted with the upper-case strings before lower-case.
This comparison function is up to 50x slower than either the default or the case-insensitive compare. It should not be used in time-critical code, but should be fast enough to sort several hundred short strings (like filenames) with a reasonable delay.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
isAlpha( str ) → boolean
boolean
isAlphaNumeric( str ) → boolean
boolean
isBreakingWhitespace( str ) → boolean
boolean
isEmpty( str ) → boolean
boolean
isEmptyOrWhitespace( str ) → boolean
boolean
isEmptyOrWhitespaceSafe( str ) → boolean
boolean
Checks if a string is null, undefined, empty or contains only whitespaces.
warning Deprecated | Use goog.string.isEmptyOrWhitespace(goog.string.makeSafe(str)) instead. |
---|
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
isEmptySafe( str ) → boolean
boolean
Checks if a string is null, undefined, empty or contains only whitespaces.
warning Deprecated | Use goog.string.isEmptyOrWhitespace instead. |
---|
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
isEmptyString( str ) → boolean
boolean
isLowerCamelCase( str ) → boolean
boolean
Returns whether the given string is lower camel case (e.g. "isFooBar").
Note that this assumes the string is entirely letters.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
| ||||
See Also | http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms |
isNumeric( str ) → boolean
boolean
Checks if a string contains only numbers.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
isSpace( ch ) → boolean
boolean
isUnicodeChar( ch ) → boolean
boolean
isUpperCamelCase( str ) → boolean
boolean
Returns whether the given string is upper camel case (e.g. "FooBarBaz").
Note that this assumes the string is entirely letters.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
| ||||
See Also | http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms |
lastComponent( str, separators ) → string
string
Finds the characters to the right of the last instance of any separator
This function is similar to goog.string.path.baseName, except it can take a list of characters to split the string on. It will return the rightmost grouping of characters to the right of any separator as a left-to-right oriented string.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
| ||||||||
See Also |
makeSafe( obj ) → string
string
Returns a string representation of the given object, with null and undefined being returned as the empty string.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
newLineToBr( str, opt_xml ) → string
string
normalizeSpaces( str ) → string
string
normalizeWhitespace( str ) → string
string
numerateCompare( str1, str2 ) → number
number
padNumber( num, length, opt_precision ) → string
string
Pads number to given length and optionally rounds it to a given precision. For example:
padNumber(1.25, 2, 3) -> '01.250' padNumber(1.25, 2) -> '01.25' padNumber(1.25, 2, 1) -> '01.3' padNumber(1.25, 0) -> '1.25'
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
parseInt( value ) → number
number
Parse a string in decimal or hexidecimal ('0xFFFF') form.
To parse a particular radix, please use parseInt(string, radix) directly. See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt
This is a wrapper for the built-in parseInt function that will only parse numbers as base 10 or base 16. Some JS implementations assume strings starting with "0" are intended to be octal. ES3 allowed but discouraged this behavior. ES5 forbids it. This function emulates the ES5 behavior.
For more information, see Mozilla JS Reference: http://goo.gl/8RiFj
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
preserveSpaces( str ) → string
string
quote( s ) → string
string
regExpEscape( s ) → string
string
Escapes characters in the string that are not safe to use in a RegExp.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
remove( str, substr ) → string
string
removeAll( s, ss ) → string
string
removeAt( s, index, stringLength ) → string
string
Removes a substring of a specified length at a specific index in a string.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
repeat( string, length ) → string
string
replaceAll( s, ss, replacement ) → string
string
Replaces all occurrences of a substring of a string with a new substring.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
Splits a string on a separator a limited number of times.
This implementation is more similar to Python or Java, where the limit parameter specifies the maximum number of splits rather than truncating the number of results.
See http://docs.python.org/2/library/stdtypes.html#str.split See JavaDoc: http://goo.gl/F2AsY See Mozilla reference: http://goo.gl/dZdZs
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
startsWith( str, prefix ) → boolean
boolean
stripNewlines( str ) → string
string
stripQuotes( str, quoteChars ) → string
string
Strip quote characters around a string. The second argument is a string of characters to treat as quotes. This can be a single character or a string of multiple character and in that case each of those are treated as possible quote characters. For example:
goog.string.stripQuotes('"abc"', '"`') --> 'abc' goog.string.stripQuotes('`abc`', '"`') --> 'abc'
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
subs( str, ...var_args ) → string
string
Does simple python-style string substitution. subs("foo%s hot%s", "bar", "dog") becomes "foobar hotdog".
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
toCamelCase( str ) → string
string
toNumber( str ) → number
number
Converts the supplied string to a number, which may be Infinity or NaN. This function strips whitespace: (toNumber(' 123') === 123) This function accepts scientific notation: (toNumber('1e1') === 10)
This is better than JavaScript's built-in conversions because, sadly: (Number(' ') === 0) and (parseFloat('123a') === 123)
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
toSelectorCase( str ) → string
string
toTitleCase( str, opt_delimiters ) → string
string
Converts a string into TitleCase. First character of the string is always capitalized in addition to the first letter of every subsequent word. Words are delimited by one or more whitespaces by default. Custom delimiters can optionally be specified to replace the default, which doesn't preserve whitespace delimiters and instead must be explicitly included if needed.
Default delimiter => " ": goog.string.toTitleCase('oneTwoThree') => 'OneTwoThree' goog.string.toTitleCase('one two three') => 'One Two Three' goog.string.toTitleCase(' one two ') => ' One Two ' goog.string.toTitleCase('one_two_three') => 'One_two_three' goog.string.toTitleCase('one-two-three') => 'One-two-three'
Custom delimiter => "-.": goog.string.toTitleCase('oneTwoThree', '-.') => 'OneTwoThree' goog.string.toTitleCase('one two three', '-.') => 'One two three' goog.string.toTitleCase(' one two ', '-.') => ' one two ' goog.string.toTitleCase('one_two_three', '-.') => 'One_Two_Three' goog.string.toTitleCase('one-two-three', '-.') => 'One-Two-Three' goog.string.toTitleCase('one...two...three', '-.') => 'One...Two...Three' goog.string.toTitleCase('one. two. three', '-.') => 'One. two. three' goog.string.toTitleCase('one-two.three', '_-.') => 'One-Two.Three'
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
trim( str ) → string
string
trimLeft( str ) → string
string
trimRight( str ) → string
string
truncate( str, chars, opt_protectEscapedCharacters ) → string
string
Truncates a string to a certain length and adds '...' if necessary. The length also accounts for the ellipsis, so a maximum length of 10 and a string 'Hello World!' produces 'Hello W...'.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
truncateMiddle( str, chars, opt_protectEscapedCharacters, opt_trailingChars ) → string
string
Truncate a string in the middle, adding "..." if necessary, and favoring the beginning of the string.
Parameters |
| ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
unescapeEntities( str ) → string
string
unescapeEntitiesWithDocument( str, document ) → string
string
urlDecode( str ) → string
string
urlEncode( str ) → string
string
URL-encodes a string
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
whitespaceEscape( str, opt_xml ) → string
string
Compiler Constants
goog.string.DETECT_DOUBLE_ESCAPING → boolean
boolean
Enables HTML escaping of lowercase letter "e" which helps with detection of double-escaping as this letter is frequently used.
goog.string.FORCE_NON_DOM_HTML_UNESCAPING → boolean
boolean
Whether to force non-dom html unescaping.