EXAMPLES OF STRINGS
- “This is a string”
- ” “
- “!05627@41&48!8” etc
You see that no matter the character, once enclosed in a quotation mark, it is treated as a string and this can be confirmed by running the
typeof("!05627@41&48!8") //string typeof(" ") //string
ALL STRING METHODS
This property returns the number of characters in a string.
let word = "Butterfly"; word.length; // 9 let sentence = "Life is not a bed of roses"; sentence.length; //26 let name = " "; name.length; // 1 empty spaces are also counted.
This method can be read out as “character at” and it returns a single character situated at that position defined as the argument. This position should be a number between 0 and 1 less than the length of the string.
This is the sister of charAt() but this doesn’t return the actual human-readable character at that specified index but the character code.
'ABC'.charCodeAt(0); // 65 'abc'.charCodeAt(0); // 97
The concat method in JS is used to concatenate two strings together to create a new string. This new string consists of the two strings joined together. In layman terms, this joins two or more strings together to form one bigger string.
let str1 = 'Hello'; let str2 = 'World'; console.log(str1.concat(str2)); // HelloWorld let str1 = 'Hello'; let str2 = 'World'; console.log(str1.concat(' ', str2)); //Hello World let str1 = 'Hello'; let str2 = 'World'; let str3 = 'Big'; console.log(str1.concat(str3, str2)); //HelloBigWorld
This determines whether one string may be found within another string. It returns either true or false as the case may be. The calling string may be the one that contains the word to search for.
let sentence = 'The quick brown fox jumps over the lazy dog.'; sentence.includes('fox'); //true sentence.includes('dog'); //false sentence.includes('Fox'); //false N.b: F is different from f.
You can also add a position to begin the search from as thus
let word = 'Hello'; word.includes('H', 2); //false;
Even though the word contains the character ‘H’, it returned false because it was asked to begin its search from index 2.
This is a very important method used in JS to return the index of the first occurrence of a specified character. index refers to the position of that character.
The index of the first character is 0, and the index of the last character of a string called
Name.length - 1. it returns -1 when it can’t find that character.
let sentence = 'The dog is eating now!'; sentence.indexOf('dog'); //4 sentence.indexOf('e'); // 2
Despite the sentence containing two “e”, it returned 2 which is the index of the first occurrence of ‘e’.
'Blue Whale'.indexOf('blue'); // returns -1 // case sensitivity
This method is the sister of indexOf() but this returns the index of the last occurrence of that character.
let word = 'Canal'; word.lastIndexOf('a'); //4 let paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?'; paragraph.lastIndexOf('dog'); // 52
This returns a string consisting of the elements of the object repeated the given times. The time is passed as an argument.
let shout = 'hey! '; shout.repeat(3); // 'hey! hey! hey! ' 'abc'.repeat(2) // 'abcabc'
This is used to replace a string with another string that matches the one specified. If there is no match, the replace function will not work.
This method extracts a section of a string and returns it as a new string, without modifying the original string. It acts as a form of “copy and paste” from a specified starting position to a specified ending position.
When no ending position is specified, the string will be copied from the specified position to the very end.
When a negative number is used, the copying starts from the back towards the beginning of the string.
split() method splits a
String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.
// split the string at the empty spaces
let sentence = "Cooking Laundry Gaming Programming Reading"; sentence.split(' '); // returns [ 'Cooking', 'Laundry', 'Gaming', 'Programming', 'Reading' ]
//split the strings at the commas
let things = 'money, cars, house, trees, phones'; things.split(','); //[ 'money', ' cars', ' house', ' trees', ' phones' ]
You can determine where the split should happen by placing that point as an argument. It could be open space, commas, anything.
This converts the string to its uppercase equivalent.
let name = 'john'; name.toUpperCase(); //JOHN let sentence = 'i am a lowercase sentence'; sentence.toUpperCase(); //'I AM A LOWERCASE SENTENCE'
This converts the string to its lowercase equivalent
'ALPHABET'.toLowerCase(); // 'alphabet' let name = "JOHN"; name.toLowerCase(); //john
trim() method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.)
let greeting = ' Hello world! '; greeting.trim(); //'Hello World'
BONUS: [ ]
You can use this to select a string at a specified position.
let word = "Hello"; word //'H' word //'e' word[word.length-1] //'o'
Given a lowercase string, convert the first letter of that string to uppercase and return the updated string.
Using the string methods at our fingerprint, we could apply so many of them to solve this. let’s take a look
//using the uppercase and slice method let name = 'peter'; let updatedName = name.toUpperCase() + name.slice(1); console.log(updatedName) //Peter
//using the uppercase and the replace method let name = 'peter'; let updatedName = name.replace(name, name.toUpperCase()); console.log(updatedName); //Peter