1function isString(value) {
2 return typeof value === 'string' || value instanceof String;
3}
4
5isString(''); // true
6isString(1); // false
7isString({}); // false
8isString([]); // false
9isString(new String('teste')) // true
10
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// String methods:
2'hello'.toUpperCase(); // "HELLO";
3'LOL'.toLowerCase(); // "lol"
4' omg '.trim(); // "omg"
5
6// String methods with arguments:
7// ==============================
8
9//indexOf returns the index where the character is found (or -1 if not found)
10'spider'.indexOf('i'); //2
11'vesuvius'.indexOf('u'); //3 - only returns FIRST matching index
12'cactus'.indexOf('z'); //-1 not found
13
14// slice - returns a "slice" of a string
15"pancake".slice(3); //"cake" - slice from index 3 onwards
16"pancake".slice(0, 3); //"pan" - slice from index 0 up to index 3
17
18// replace - returns a new string, with the FIRST match replaced
19"pump".replace("p", "b"); //"bump" - only replaces first "p"
20
21// String Template Literals
22// Use backtick characters, NOT SINGLE QUOTES!