JavaScript - Strings
- A sequence of zero or more Unicode characters enclosed by single or double quotations
- JavaScript strings (and arrays) are indexed from zero (0)
- Integer to String Conversion
parseInt(string, radix)- A function which accepts a string and returns an integer in the specified base.
- Radix (e.g. base) is an optional parameter. Valid values are from 2-36. It defaults to 10.
- Only the first number of the string is returned
- Leading and trailing spaces are ignored
- If the 1st character cannot be converted to a number, NaN is returned
- Concatenation
- Built into the core JavaScript language is the ability to concatenate strings via the + operator
var s = "Hello" + " World"
- Built into the core JavaScript language is the ability to concatenate strings via the + operator
- Length
- To determine the length of a string, use the length() method of the String class
var len_x = x.length();
- To determine the length of a string, use the length() method of the String class
- Substrings
- To extract a substring, use the substring() method of the String class
var s1 = x.substring(1,5);
- To find the position of the first occurance of a substring use the
indexOf()method of the String classvar pos = indexOf('W');
- To extract a substring, use the substring() method of the String class
- Unlike other languages if the backslash is followed by any character other than the ones noted in the above table, it's simply ignored
- Escape Sequences
Sequence Interpretation \0 NULL character \b Backspace \t Tab \n Newline \v Vertical tab \f Form Feed \r Carriage Return \" Double Quote \' Single Quote \\ Backslash \xXX Latin-1 character specified by hexadecimal # XX \uXXXX Unicode character specified by XXXX