String
String is very useful object for showing character. Such as, alphabet, any name, paragraph ect.
How to create a string?
let string = “Tahiya Jahan Laboni”
Here, Tahiya Jahan Laboni is a name which is a string and for creating this string in javascript we need to insert this name in “ ” apostrophe. And if you want to find the length of this string, just use string.length
This should return 19, because here “Tahiya Jahan Laboni” has 17 character and also have 2 white space which also individual character.
Find specific string character:
For finding specific character, you should include square brackets [] at the end of the string variable name.
For using the number of 0 in square brackets [], it will return first character which is T. Computer always start counting from 0. So, for finding first character, you should use 0, not 1.
let stringLastElement = string[stringLength-1]
For finding last character, you should use stringLength-1 in square bracket [], because, we knew, there have 19 character. Computer start counting from 0, But for counting length, this start from 1.
Character Access:
If you want to show a specific character which you need, there have two ways:
Here, which character you need, you just insert in () or [].
1. concat():
Concat method uses to merge multiple arrays.
Using concat method, create a new string and return this. the existing method which is firstName and lastName is not going to be changed.
concat() three array:
for merge three arrays, use this firstName.concat(middleName,lastName);
concat() nested array:
the following code concatenates nested arrays:
2. endsWith() Method:
This method used for searching whether a string ends and return Boolean result true or false.
syntex:
endsWith(search)
endsWith(search, length)
for example:
3. indexOf() method:
indexOf() method returns the position number of searching string if the given element can be found in the array or string. otherwise return -1.
Syntax:
string.indexOf(searchingValue, start)
array.indexOf(searchingValue, start)
here, first parameter searchingValue is required and start parameter is optional.
Example:
4. lastIndexOf() method:
This method returns the last matching index number . If searching value doesn’t match, return -1
We see, there have 3 Laboni. lastIndexOf() return the last Laboni index. here, this Laboni starting index is 19. so , it’s returned 19.
syntex:
string.lastIndexOf(searchingValue, start)
here, first parameter searchingValue is required and start parameter is optional.
example:
5. replace() method:
this method searches a string for finding a specified value or regular expression, and replace this a new string.
this method doesn’t change the original string.
syntax:
string.replace(searchingValue, newValue)
here, searchingValue and newValue both are required.
Global replace():
In the following example, the regular expression defined and includes the global case:
Global and ignore with replace()
In the following example, the regular expression defined and includes the global and global ignore case:
Using a function for replace()
In the following example, the regular expression defined using function()
6. slice() method:
The slice() method defined with start and end value. It’s return a new array object selected from start to end. Here, end is not required. If end is not insert in slice() method, then it will return start to end of the string or array value.
syntax:
slice()
slice(start)
slice(start, end)
7. split() method:
spliit() method used to split a string and return a new array.
Here, if we use split(“ ”) , this method will be ignore the white space and find the string without count white space. For split(“”) method, find every character in the string. And split() method finding the array of the string.
syntax:
string.split(separator, limit)
here, separator parameter is optional. this parameter use for specifies the character and it’s also use for regular expression. if we don’t use this parameter, this return entire string. and limit also optional.
Example of limited number of split():
const string = 'a b c d e f g h';
const limitedSplits = string.split(' ', 4); // [ 'a', 'b', 'c', 'd']
console.log(limitedSplits);
Here, limit value is 4. so, we can find a array with ignore white space and starting string a to d.
8. startsWith() method:
This method used for searching whether a string begin or start and return Boolean result true or false.
syntax:
string.startsWith(searchingValue, start)
here, first parameter searchingValue is required and start parameter is optional.
9. toLowercase() method
This method return the string with lower letters. this method doesn’t change the original string.
syntax:
string.toLowerCase()
10. toUppercase() method
This method return the string with upper letters. this method doesn’t change the original string.
syntax:
string.toUpperCase()
11. trim() method
This trim() method removes white spaces from both sides of a string.
syntax:
string.trim()
if we use trimStart() method, this method removes white space from the beginning and trimEnd() method removes whte space from the end of the string.
// trimStart() and trimEnd()var trimString = " Tahiya jahan Laboni "console.log(trimString.trim()) //Tahiya jahan Laboniconsole.log(trimString.trimStart()) //"Tahiya jahan Laboni "console.log(trimString.trimEnd()) //" Tahiya jahan Laboni"