While developing Javascript applications we generally need some type that can hold text and string data. We call this type of variable as a string. The string can hold names, street info, paragraph, explanation, etc. The string is the type used for text data. String variables can be defined in various ways.
name="Poftut"; name2='Poftut';
- These are string variables
var val = new String("Poftut");
- val is a String object the very similar to a string variable
There are methods to use with the string data type. The string data type is wrapped as a String object in Javascript to provide this method.
Get The Length of String
To get the length of the string is easy as below. Length is equal to the number of characters including spaces.
Code:
var val = "Poftut"; console.log(val.length); var val = "Poftut "; console.log(val.length);
- length provides the length of string
Output:
6 7
- As we see the second line of output is 7 where the second string has space at the end
Find Index of String
If we are looking at a characters position in the string we can use indexOf function
Code:
poem="This is a poem from me"; console.log(poem.indexOf("p")); console.log(poem.indexOf("z"));
Output:
10 -1
- If the char is not existed in the string -1 is returned.
Combine Strings
As we know strings cannot be changed after initialized so to add some string concat can be used
Code:
poem="This is a poem from me."; newpoem=poem.concat(" This is added"); console.log(newpoem);
Output:
This is a poem from me. This is added
Return A Char With Index
To get a character from the specified index charAt function is used. Index numbers start from 0.
Code:
poem="This is a poem from me"; console.log(poem.charAt(1));
Output:
h
Replace String
To replace the string replace method is used. There are two parameters.
Code:
poem="This is a poem from me"; console.log(poem.replace("me","you"));
- “me” is the string we want to replace
- “you” the new string
Output:
This is a poem from you
Search String
Searching for a specified string in a string. Returns the index of the search term.
Code:
poem="This is a poem from me"; console.log(poem.search("me"));
Output:
20
Substring
There are two types of substring method. Substring is used to extract some part of a string by providing index values.
Code:
poem="This is a poem from me"; console.log(poem.substring(1,2));
- 1 starts index for a substring
- 2 ends index for a substring
Output:
h
Code:
poem="This is a poem from me"; console.log(poem.substr(1,2));
- 1 starts index for a substring
- 2 is the length of the substring from start index which means we will get 2 characters
Output:
hi
To Lower Case
All uppercase characters can be converted into lowercase characters with toLowerCase method
Code:
poem="This Is a Poem From Me"; console.log(poem.toLowerCase());
Output:
this is a poem from me
To Upper Case
All lowercase characters can be converted into uppercase characters with toUpperCase method
Code:
poem="This Is a Poem From Me"; console.log(poem.toUpperCase());
Output:
THIS IS A POEM FROM ME
Convert To String
We may need numeric or other data types to convert string to manipulate them.
Returned object type will be a string
Code:
count=5; console.log(count.toString());
Output:
5