Strings are one of the commonly used Javascript data types. Javascript strings are used to store data represented in text form written with single, double, or back-tick quotation marks. To know more about strings please refer to this article on W3schools.
In this article, you will learn about popular methods used to manipulate data in strings.
1. length()
To find characters in a string, use the length() method. Remember string indexes start from 0
const text= "Hello"
const text1 ="Worlds"
text.length() //4
text1.length() //5
In our example, the length of text and text1 is 4,5 respectively.
2.slice()
The slice method will extract characters from specified start-point and endpoint indexes, excluding the end index. It picks the one before it to create a new string. Acts like the subString()
const text= "Hello"
const text1 ="Worlds"
text.slice(1,4)
let newtext= "ell"
3.subStr()
These two methods behave much like the slice method. The only difference is the second parameter specifies the number of characters to slice
const text= "Hello"
const text1 ="Worlds"
text.substr(3,2) //lo
4. toUpperCase()
Converts all characters in a string to uppercase.
const text= "Hello"
const text1 ="Worlds"
text.toUpperCase() //HELLO
4. toLowerCase()
Converts all characters in a string to lowercase
const text= "Hello"
const text1 ="Worlds"
text.toUpperCase() //HELLO
5. concat()
The concat() method is used instead of the plus sign + used before in previous versions of Javascript. It is an easier way of joining a string
const text= "Hello"
const text1 ="Worlds"
text.concat(" ",text1)
let newText="Hello World"
6. charAt()
The charAt() method returns a character of a specified index in a string
const text= "Hello"
const text1 ="World"
text.charAt(3) //l
7. replace()
The replace() method places new characters in a string in place of old ones. The first parameter is the value to be searched for and the second is the value to replace it. It returns a new string with the replaced values
const text= " New Hello"
const text1 ="World"
text.replace("Hello","car")
let newText="New car"
8. repeat()
The repeat() method takes a number as an argument and it returns the repeated version of the string.
const text= " New car"
let newText=text.repeat(2) //New car New car
9. split() and split('')
The split() method converts a string into an array. This makes it easier for the array to be manipulated.
const text= " Hello"
text.split() //['Hello']
The split('') converts each character into a string in an array.
text.split(' ') //['H', 'e', 'l', 'l', 'o']
10. includes()
The include method checks whether a character is in the given string and returns a boolean value(True/False)
const text= " Hello"
text.includes(r) //false
11.typeof()
The typeof() method checks for the datatype of a given variable. Because Javascript is dynamic,variable values can change over time. Hence it's advisable to keep checking the type of values so it does not throw an error.
let firstName = 'Sandra'
let age = 34
console.log(typeof firstname) //string
console.log(typeof age) //number
Casting-changing data types from one type to another.
In strings, sometimes data has to change from one form to another so that it can be reused without throwing an error. Change string to a number or integer using the following methods:
- parseint()
- Number()
- -plus sign
12. parseInt()
let num = '12'
let numInt = parseInt(num) // 12
Also, we can convert float numbers to integers. Use the following method to convert float to the nearest int. parseInt()
let num = 5.11
let numInt = parseInt(num) //5
13. Number()
let num = '10.1'
let numFloat = Number(num) //10.1
14. + ()plus sign
Adding a plus sign next to the variable will change the variable into a positive number
let num = '5.11'
let numFloat = +num //5.11
Conclusion
I hope you have learned something from this article. Please feel free to add more string methods to the thread.