goog.string.Const.from
is only invoked on
compile-time-constant expressions.
Const is useful in APIs whose correct and secure use requires that certain
arguments are not attacker controlled: Compile-time constants are inherently
under the control of the application and not under control of external
attackers, and hence are safe to use in such contexts.
Instances of this type must be created via its factory method
goog.string.Const.from
and not by invoking its constructor. The
constructor intentionally takes no parameters and the type is immutable;
hence only a default instance corresponding to the empty string can be
obtained via constructor invocation.
goog.html.SafeHtml
. If available, client code that
needs to ensure type membership of an object should use the type's function
to assert type membership, such as goog.html.SafeHtml.unwrap
.
![]()
Common Unicode string characters.
Constants:
|
Code » |
Concatenates string expressions. This is useful
since some browsers are very inefficient when it comes to using plus to
concat strings. Be careful when using null and undefined here since
these will not be included in the result. If you need to represent these
be sure to cast the argument to a String first.
For example:
buildString('a', 'b', 'c', 'd') -> 'abcd' buildString(null, undefined) -> ''
Arguments:
Returns: string
The concatenation of
var_args .
|
code » | |||||
Replaces Windows and Mac new lines with unix style: \r or \r\n with \n.
|
code » | |||||
A string comparator that ignores case.
-1 = str1 less than str2
0 = str1 equals str2
1 = str1 greater than str2
|
code » | |||||
Determines whether a string contains a substring, ignoring case.
|
code » | |||||
Case-insensitive suffix-checker.
|
code » | |||||
Case-insensitive equality checker.
|
code » | |||||
Case-insensitive prefix-checker.
|
code » | |||||
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.
|
code » | |||||
Converts multiple whitespace chars (spaces, non-breaking-spaces, new lines
and tabs) to a single space, and strips leading and trailing whitespace.
|
code » | |||||
Compares elements of a version number.
|
code » | |||||
Compares two version numbers.
|
code » | |||||
Determines whether a string contains a substring.
|
code » | |||||
Returns the non-overlapping occurrences of ss in s.
If either s or ss evalutes to false, then returns zero.
|
code » | |||||
Generates and returns a string which is unique in the current document.
This is useful, for example, to create unique IDs for DOM elements.
Returns: string
A unique id.
|
code » | |||||
Fast suffix-checker.
|
code » | |||||
Takes a character and returns the escaped string for that character. For
example escapeChar(String.fromCharCode(15)) -> "\\x0E".
|
code » | |||||
Takes a string and returns the escaped string for that character.
|
code » | |||||
Performs sprintf-like conversion, ie. puts the values in a template.
DO NOT use it instead of built-in conversions in simple cases such as
'Cost: %.2f' as it would introduce unneccessary latency oposed to
'Cost: ' + cost.toFixed(2).
|
code » | |||||
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.
Returns: string
A random string, e.g. sn1s7vb4gcic.
|
code » | |||||
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).
|
code » | |||||
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.
Arguments:
Returns: string
An escaped copy of
str .
|
code » | |||||
Checks if a string contains all letters.
|
code » | |||||
Checks if a string contains only numbers or letters.
|
code » | |||||
Checks if a string is all breaking whitespace.
|
code » | |||||
Checks if a string is empty or contains only whitespaces.
|
code » | |||||
Checks if a string is null, undefined, empty or contains only whitespaces.
Arguments:
Returns: boolean
True if
str is null, undefined, empty, or
whitespace only.
|
code » | |||||
Returns whether the given string is lower camel case (e.g. "isFooBar").
Note that this assumes the string is entirely letters.
|
code » | |||||
Checks if a string contains only numbers.
Arguments:
Returns: boolean
True if
str is numeric.
|
code » | |||||
Checks if a character is a space character.
|
code » | |||||
Checks if a character is a valid unicode character.
|
code » | |||||
Returns whether the given string is upper camel case (e.g. "FooBarBaz").
Note that this assumes the string is entirely letters.
|
code » | |||||
Returns a string representation of the given object, with
null and undefined being returned as the empty string.
Arguments:
Returns: string
A string representation of the
obj .
|
code » | |||||
Converts \n to
s or s. |
code » | |||||
Normalizes spaces in a string, replacing all consecutive spaces and tabs
with a single space. Replaces non-breaking space with a space.
|
code » | |||||
Normalizes whitespace in a string, replacing all whitespace chars with
a space.
|
code » | |||||
String comparison function that handles numbers in a way humans might expect.
Using this function, the string "File 2.jpg" sorts before "File 10.jpg". 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 significantly slower (about 500x) 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.
|
code » | |||||
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' |
code » | |||||
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
|
code » | |||||
Preserve spaces that would be otherwise collapsed in HTML by replacing them
with non-breaking space Unicode characters.
|
code » | |||||
Encloses a string in double quotes and escapes characters so that the
string is a valid JS string.
|
code » | |||||
Escapes characters in the string that are not safe to use in a RegExp.
Arguments:
Returns: string
A RegExp safe, escaped copy of
s .
|
code » | |||||
Removes the first occurrence of a substring from a string.
|
code » | |||||
Removes all occurrences of a substring from a string.
|
code » | |||||
Removes a substring of a specified length at a specific
index in a string.
Arguments:
Returns: string
A copy of
s with the substring removed or the full
string if nothing is removed or the input is invalid.
|
code » | |||||
Repeats a string n times.
|
code » | |||||
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
|
code » | |||||
Fast prefix-checker.
|
code » | |||||
Takes a string and replaces newlines with a space. Multiple lines are
replaced with a single space.
|
code » | |||||
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' |
code » | |||||
Does simple python-style string substitution.
subs("foo%s hot%s", "bar", "dog") becomes "foobar hotdog".
|
code » | |||||
Converts a string from selector-case to camelCase (e.g. from
"multi-part-string" to "multiPartString"), useful for converting
CSS selectors and HTML dataset keys to their equivalent JS properties.
|
code » | |||||
Takes a string and creates a map (Object) in which the keys are the
characters in the string. The value for the key is set to true. You can
then use goog.object.map or goog.array.map to change the values.
|
code » | |||||
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)
|
code » | |||||
Converts a string from camelCase to selector-case (e.g. from
"multiPartString" to "multi-part-string"), useful for converting JS
style and dataset properties to equivalent CSS selectors and HTML keys.
|
code » | |||||
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'
Arguments:
Returns: string
String value in TitleCase form.
|
code » | |||||
Trims white spaces to the left and right of a string.
|
code » | |||||
Trims whitespaces at the left end of a string.
|
code » | |||||
Trims whitespaces at the right end of a string.
|
code » | |||||
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...'.
|
code » | |||||
Truncate a string in the middle, adding "..." if necessary,
and favoring the beginning of the string.
Arguments:
Returns: string
A truncated copy of
str .
|
code » | |||||
Unescapes an HTML string.
|
code » | |||||
Unescapes an HTML string using a DOM to resolve non-XML, non-numeric
entities. This function is XSS-safe and whitespace-preserving.
|
code » | |||||
Unescapes a HTML string using the provided document.
|
code » | |||||
Unescapes XML entities.
|
code » | |||||
URL-decodes the string. We need to specially handle '+'s because
the javascript library doesn't convert them to spaces.
|
code » | |||||
URL-encodes a string
Arguments:
Returns: string
An encoded copy of
str that is safe for urls.
Note that '#', ':', and other characters used to delimit portions
of URLs *will* be encoded.
|
code » | |||||
Do escaping of whitespace to preserve spatial formatting. We use character
entity #160 to make it safer for xml.
|
code » |
![]()
Regular expression that matches any character that needs to be escaped.
|
Code » | |
![]()
Regular expression that matches an ampersand, for use in escaping.
|
Code » | |
![]()
No description.
|
Code » | |
![]()
Regular expression that matches a lowercase letter "e", for use in escaping.
|
Code » | |
![]()
Regular expression that matches a greater than sign, for use in escaping.
|
Code » | |
![]()
Maximum value of #goog.string.hashCode, exclusive. 2^32.
|
Code » | |
![]()
Regular expression that matches an HTML entity.
See also HTML5: Tokenization / Tokenizing character references.
|
Code » | |
![]()
Regular expression that matches a less than sign, for use in escaping.
|
Code » | |
![]()
Regular expression that matches null character, for use in escaping.
|
Code » | |
![]()
Regular expression that matches a double quote, for use in escaping.
|
Code » | |
![]()
Regular expression that matches a single quote, for use in escaping.
|
Code » | |
![]()
No description.
|
Code » | |
![]()
No description.
|
Code » | |
![]()
No description.
|
Code » | |
![]()
No description.
|
Code » | |
![]()
Character mappings used internally for goog.string.escapeChar.
|
Code » | |
![]()
No description.
|
Code » | |
![]()
No description.
|
Code » | |
![]()
No description.
|
Code » | |
![]()
No description.
|
Code » | |
![]()
Regular expression used for splitting a string into substrings of fractional
numbers, integers, and non-numeric characters.
|
Code » | |
![]()
No description.
|
Code » | |
![]()
No description.
|
Code » | |
![]()
Special chars that need to be escaped for goog.string.quote.
|
Code » | |
![]()
The most recent unique ID. |0 is equivalent to Math.floor in this case.
|
Code » |