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
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"