1var abc = "abcdefghijklmnopqrstuvwxyz";
2var esc = 'I don\'t \n know'; // \n new line
3var len = abc.length; // string length
4abc.indexOf("lmno"); // find substring, -1 if doesn't contain
5abc.lastIndexOf("lmno"); // last occurance
6abc.slice(3, 6); // cuts out "def", negative values count from behind
7abc.replace("abc","123"); // find and replace, takes regular expressions
8abc.toUpperCase(); // convert to upper case
9abc.toLowerCase(); // convert to lower case
10abc.concat(" ", str2); // abc + " " + str2
11abc.charAt(2); // character at index: "c"
12abc[2]; // unsafe, abc[2] = "C" doesn't work
13abc.charCodeAt(2); // character code at index: "c" -> 99
14abc.split(","); // splitting a string on commas gives an array
15abc.split(""); // splitting on characters
16128.toString(16); // number to hex(16), octal (8) or binary (2)
17
1JS String Methods:
2charAt()
3Returns a character at a specified position inside a string
4charCodeAt()
5Gives you the unicode of character at that position
6concat()
7Concatenates (joins) two or more strings into one
8fromCharCode()
9Returns a string created from the specified sequence of UTF-16 code units
10indexOf()
11Provides the position of the first occurrence of a specified text within a string
12lastIndexOf()
13Same as indexOf() but with the last occurrence, searching backwards
14match()
15Retrieves the matches of a string against a search pattern
16replace()
17Find and replace specific text in a string
18search()
19Executes a search for a matching text and returns its position
20slice()
21Extracts a section of a string and returns it as a new string
22split()
23Splits a string object into an array of strings at a specified position
24substr()
25Similar to slice() but extracts a substring depended on a specified number of characters
26substring()
27Also similar to slice() but can’t accept negative indices
28toLowerCase()
29Convert strings to lowercase
30toUpperCase()
31Convert strings to uppercase
32valueOf()
33Returns the primitive value (that has no properties or methods) of a string object
1// Making Strings
2let color = "purple";
3
4// Single quotes work too:
5let city = 'Tokyo';
6
7//Strings have a length:
8city.length; //5
9
10//We can access specific characters using their index:
11city[0]; //'T'
12city[3]; //'y'
13
14// String methods:
15'hello'.toUpperCase(); // "HELLO";
16'LOL'.toLowerCase(); // "lol"
17' omg '.trim(); // "omg"
18
19// String methods with arguments:
20// ==============================
21
22//indexOf returns the index where the character is found (or -1 if not found)
23'spider'.indexOf('i'); //2
24'vesuvius'.indexOf('u'); //3 - only returns FIRST matching index
25'cactus'.indexOf('z'); //-1 not found
26
27// slice - returns a "slice" of a string
28"pancake".slice(3); //"cake" - slice from index 3 onwards
29"pancake".slice(0, 3); //"pan" - slice from index 0 up to index 3
30
31// replace - returns a new string, with the FIRST match replaced
32"pump".replace("p", "b"); //"bump" - only replaces first "p"
33
34// String Template Literals
35// Use backtick characters, NOT SINGLE QUOTES!
36// ========================
37const color = "olive green";
38const msg = `My favorite color is: ${color}` //"My favorite color is: olive green"
39
40const str = `There are ${60 * 60 * 24} seconds in a day`//"There are 86400 seconds in a day"