2. Finding String Length
A String is an array of characters. Finding length means you are finding a number of characters present in the string. Have a look at the example below:
1) A String is represented by the pack of characters enclosed between “.
2) The length property gives you a number of characters in the string.
3) Shows the output.
3. Find String Position based on indexOf, lastIndexOf
We can search a specific string inside a main string. Below is the example in which we search for the string ‘Kalyan’.
1) The function
indexOf takes a string that needs to be searched. It returns the character position starting the count from Zero. Note, when the string is found multiple time, it returns the index position of the first string.
2) String also provides a function to find the last occurrence of the string. Here, the string Kalyan appears two times and lastIndexOf function gets the index position of the last occurrence.
3) Shows the output. Note, the count starts from zero while doing the search.
4. Cut a Sub-String from Main – subString, subStr
Sometimes we need to get the part of the string instead of finding its position. The example is below:
2) The function subStr also fetches the part of a string. First parameter tells the start of the string and the second one tells length or how many chars to include from the start position.
3) Shows the output by executing the example.
One can convert the Uppercase string to lowercase and vice versa. The example is below:
1) The function
toUpperCase converts the string into uppercase letters.
2) Shows the uppercase converted string.
3) The function toLowerCase converts all the characters into the lowercase.
4) Shows the lower case converted string.
6. Get Char Code and Character at a Location
1) We know the variable
PersonName holds the value “Jim Wilson” (Refer Section 2). The function
charAt takes the position in the string and in our case, it is 2. Since the char count starts at zero, the function picks the letter m from the string.
2) The function charCodeAt does the same thing but converts the char into ASCII format and gives the number 109.
3 & 4) Shows the result
1) The function
fromCharCode gets a comma separated list of char codes. In our example, we passed six-character codes and stored the returned result in a string
2) Shows the output.
8. Cut down Leading & Trailing Space
Sometimes, you may need to cut the space in the front and back of the string. Below is the example:
1) Shows the string is have a leading space.
2) Shows the same string having the trailing space as well.
3) The trim function of a string cut downs both leading and trailing spaces.
4) Shows the string output after removing the spaces.
//Example 01: Length of the String
var PersonName = "Jim Wilson";
document.write("String Length: " + PersonName.length + "</br>");
//Example 02: Finding SubString Position
var String2 = "My name is Kalyan Kumar. Shortly Kalyan";
var Pos1 = String2.indexOf("Kalyan");
var Pos2 = String2.lastIndexOf("Kalyan");
document.write("First Position of Kalyan " + Pos1 + "</br>");
document.write("Last Position of Kalyan " + Pos2 + "</br>");
//Example 03: Cut Substring from Main String
document.write(String2.substring(3,7) + "</br>");
document.write(String2.substr(3,4) + "</br>");
//Example 04: Case Conversion
document.write(String2.toUpperCase() + "</br>");
document.write(String2.toLowerCase() + "</br>");
//Example 05: Get Char and Char-Code
document.write("Person Name is " + PersonName + "</br>");
document.write("ChartAt(2) = " + PersonName.charAt(2) + "</br>");
document.write("ChartCodeAt(2) = " + PersonName.charCodeAt(2) + "</br>");
//Example 06: Get String from Char-Code
var Result1 = String.fromCharCode(65,66,67,68,69,70);
document.write("Resulting String is " + Result1 + "</br>");
//Example 07: Remove Leading & Trailing Space
var StringWithSpace = " This is a String ";
document.write("Trimmed String : " + StringWithSpace.trim());
Tags: charAt & charCodeAt, fromCharCode, indexOf & lastIndexOf, String, subString & subStr, toUpperCase & toLowerCase, trim
Do you like this Example? Please comment about it for others!!