Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JavaScript 011 – Basic String Functions

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:

JavaScript String Length
JavaScript String Length

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’.

JavaScript Find String Position
JavaScript Find String Position

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:

JavaScript Get Sub-String
JavaScript Get Sub-String

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:

JavaScript String Uppercase Lowercase
JavaScript String Uppercase Lowercase

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:

JavaScript Get Single Char in a String
JavaScript Get Single Char in a String

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:

JavaScript Get String From Char-Code
JavaScript Get String From Char-Code

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:

Javascript Trimming a String
Javascript Trimming a String

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

Categories: JavaScript

Tags: , , , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.