Javascript String
This is one of javascript data types, it is represented with text and is written in double(“ ”)or single quotes(‘ ’). It’s zero or more characters wrapper inside a quote
Example:
let firstName=”Chisom”; //double quotelet firstName=’Chisom’; //single quotelet regNumber=’2455aerr’; //combination of text and numbers
STRING LENGTH
To check the length of a string, we use the length property.
Let’s try to find the length of x;
let x =”she is a good girl”;console.log(x.length)// result is 18
Kindly note that the spaces are counted as well.
Quote inside String Quotes
You can have a quoted statement inside a string quote as long as the quote you are using doesn’t match the enclosing quote but If you need to make a single quote in a single quote string or a double quote inside a double string, you will need to use the backlash property (\) to render it else the computer wouldn’t be able to read it properly.
Some Illustrations:
let cheers=”everybody say ‘cheers to the new couple’” //correctLet cheers=’everybody say “cheers to the new couple”’ //correctlet cheers= “everybody say “cheers to the new couple” “. // error Xlet cheers= “everybody say \”cheers to the new couple\”” // correctlet cheers= ‘everybody say ‘cheers to the new couple’’ //error Xlet cheers=’everybody say \‘cheers to the new couple\’’ //correctLet cheers=’we\’ve heard you’ //correct
Finding Position of a string inside a string
To find the position of a string in a string we use the indexOf(). The bracket holds the string which position we are trying to find.
let cheers =’everybody say “cheers to the new couple” ’
Let’s say we want to find the position of cheers in the example above, we do:
console.log(indexOf(“cheers”))//result is 15
LastIndexOf()
To get the last occurrence of a string in a string, we use the lastIndexOf(). Its searches the string from the end to the beginning but returns the position from the beginning starting from 0
For instance:
let good= “she is a good girl that is good and good to others who are Good”;
In order to console log the last occurrence of good in the above string, we do:
console.log(good.lastIndexOf(“good”))// result 36.
indexOf and lastIndexOf are cases sensitive, so if the search is for “good”, it will overlook “Good”
If you search for a string that does not exist in a string, you will get back -1 for both indexOf and lastIndexOf
Extracting Parts of String
To extract parts of a string, we use the slice() method, the slice method accepts two parameters which are the index of where the extraction will start from and the index of where the extraction will end.
For Example:
let gender= “Obi is a boy and Ada is a girl”
If I wanted to extract from boy to Ada, I would have to count from the beginning of the string to the first word of boy to get it’s index and from the beginning of the string to the point just after Ada. For this example, the index of boy(counting from beginning to b) is 9 and the index of Ada(counting from the beginning to after a) is 20.
So to console.log the result, we will do:
gender.slice(9,20) // result is boy and Ada
Another way extracting can be done is the substr() method, this also accepts 2 parameters. But in this case, you are extracting a fixed number of characters; the first parameter is the index of the string you want to start the extraction from while the second parameter starts counting from the index of the first parameter to the index of the end of extraction.
console.log(gender.substr(9,20)) // result is boy and Ada is a girl
You can extract just one character using the charAt() method
console.log(gender.charAt(9))
Concatenation Of Strings
This means joining two or more strings and it is done using the assignment operators = and +=.
Example:
let traffic= ‘it is green;let command = ‘Go!’console.log(traffic + ‘ ‘ + command )// this returns “it is green Go!Example 2:Let traffic= ’its is green’traffic += ‘ Go!’ // make sure to leave a space after the opening quotation mark if you need for there to be spaceconsole.log(traffic) this returns “it is green Go
Replacing String Content
You can take out a character in a string and replace with another with the replace() method. This takes 2 parameters:the string you want to change and the one to replace it with.
Example:
let traffic=’it is red, you can go’console.log(traffic.replace(‘red’, ‘green’)) // this returns ’it is green, you can go’
You can also look at other javascript data types like Object, Numbers, Array, Boolean