1. JavaScript String
In this JavaScript Example, we will learn the basic functionality of the String. JavaScript exposes it as an object, and you can perform manipulation based on the exposed functions. In this first part of the examples, we will learn some of the basic string operations.
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:

1) We use the same string from section 3. JavaScript function
subString cut the string based on start and end position. The first parameter tells the start position, and the second parameter tells the end position. Here, 3 specified the start location, which is index location 3 and 7 specifies the trailing location. When JavaScript cuts the string, it will not include the character at index location 7.
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.
5. JavaScript Case Conversion
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
JavaScript string can also retrieve the character at a given location. You can also get the character as an ASCII number as well. Have a look at the below example:

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
7. Form JavaScript String from Char-Code
It is also possible to form a JavaScript String from the set Char-Codes. Below is an example:

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 Result1
.
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.
Code Reference
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<!DOCTYPE html> <html lang="en"> <head> <title>Learning JavaScript String Object</title> </head> <body> <script> //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()); </script> </body> </html> |
Categories: JavaScript
Tags: charAt & charCodeAt, fromCharCode, indexOf & lastIndexOf, String, subString & subStr, toUpperCase & toLowerCase, trim