JQuery String Functions (Replace, Substr, IndexOf, lastIndexOf, Substring, …)

I remember those day when I am a fresher to programming. To complete my assignments related to string operations I always take help from Google. Sometime to find out a Syntax or Sometime for an Examples. In my view string operations are little bit confusing to freshers. During programming there are several possibilities to do with a string. Some of them are String Concatenation, Finding the position of a Character in a String, Split or retrieve few part of the String.

Let me explain a senario here where you need string operation. Assume from your database you are fetching product descriptions for each products in a HTML page. During this you don’t want to show whole the product description texts. As per the customer you need to show first 40 charecters from product description string. In this case you need to go for string operations using string functions like substr or substring to extract a part of the string.

List of JQuery String Functions

In this session for beginner of Jquery to make their journey easier I am explaining all the Jquery String Functions in details. The programming language I used here is Jquery. But keep remember theoretically fundamentals of String Operations are same for all languages. In implementation only Syntax’s are differ.

charAt(n)

In a String array to know the Character of a specific index we use charAt() function. Let’s take an example you have a string “THIS IS A TABLE.”. In this String if I want to know the Character of index 3 charAt() function can did that. Look the example below.

var myStr = "THIS IS A TABLE.";
var ch = myStr.charAt(3);

Output will be : S

charAt() function accepts integer value as the param & returns String Character. charAt(n) calculates index starting from 0.

String Function charCodeAt(n)

charCodeAt() works similarly like chatAt() function. The only difference is while charAt() returns Character of a specific index charCodeAt() returns Unicode of the Character. Look at the example below.

var myStr = "THIS IS A TABLE.";
var ch = myStr.charCodeAt(1);

Output will be : 72

72 is the Unicode for Capital H. charCodeAt() accepts integer as the param & returns Unicode value.

concat(string1, string2, .., stringN)

String Concatenation is a very common used String operation. Let’s take a case where in two variables you are getting name of a person. First Name & Last Name. In this case to display the whole name we required to do string Concatenation operation. Look at the example below.

var strFirstName = "Biswabhusan ";
var strLastName = "Panda";
var wlcmMsg = "You are welcome!";
var ch = strFirstName.concat(strLastName, " ", wlcmMsg);

Output will be : Biswabhusan Panda You are welcome!

Using Jquery concat() method (Originally derived from JavaScript ) you can concatenate n number of strings. This method accepts n number of strings as parameters. To apply this method choose the first string variable & then concatenate others.

fromCharCode(n1, n2, …, nX)

Refer to the method name fromCharCode() it Converts Unicode value to Character. This is a static method of String object. To use this method you can use String.fromCharCode(). Look at the example below.

var ch = String.fromCharCode(67);

Output will be : C

This function accepts n number of Unicode values.

String Function indexOf(searchvalue, [start])

indexOf() method returns the position of a specific value in a string. It return -1 if the value not found in string. While checking the value in string this method considers uppercase & lowercase characters. Using this method we can know whether a specific word exist in the string or not. Look at the example below.

var myStr="Hello, You are welcome to the my blog.";
var ch=myStr.indexOf("welcome");

Output will be : 14

indexOf() accepts 2 parameters. First parameter is the keyword you want to search in the string. Second parameter is from which index you want to search rest of the string. By default this is 0. 0 means you are searching the string from the beginning.

lastIndexOf(searchvalue, [start])

lastIndexOf() method work in similar fashion of indexOf() method. The only difference is it shows the searched value position at the last occurrence of the string. For an example if in a String “welcome” appears two times lastIndexOf() method returns the 2nd occurrence of welcome. If searched value not exist in the string it returns -1. This is case sensitive. Welcome is not equal to welcome. While returning the index it counts from the beginning of the string.

var myStr="This is Biswabhusan Panda, You can only Call Biswabhusan.";
var ch=myStr.lastIndexOf("Biswabhusan");

Output will be : 44

lastIndexOf() accepts 2 parameters like indexOf() method. First parameter is the keyword you want to search in the string. Second parameter is from which index you want to search rest of the string. By default this is 0. 0 means you are searching the string from the beginning.

substr(start, [length])

substr() string function used to retrieve part of a string. This function accepts 2 parameters start & length in integer value. Start says from which index you want the part of the string. Length is the number of characters you want from the start index. Let’s take an example. I have a string “My Car is blue.”. In this string if I am applying substr(2, 3) it will return Car.

var myStr="My Car is blue.";
var ch=myStr.substr(2, 3);

Output will be : Car

Popular String Function substring(from, [to])

substring() works similarly like substr(). But while substr() accepts Start & Length as the parameters substring() accepts From & To. From is the index from which you want your part of the string. To is the index upto which you want to retrieve in part of the string. Let’s take the same example as shown in substr. You can see the difference while substr(2, 3) return “Car” substring(2, 3) will return “C”.

var myStr="My Car is blue.";
var ch=myStr.substring(2, 3);

Output will be : C

toLowerCase()

toLowerCase() converts uppercase letters to lowercase. If in a string you have mix of upper & lower case letter this function will return that string in lowercase only.

var myStr="Biswabhusan";
var ch = myStr.toLowerCase();

Output will be : biswabhusan

toUpperCase()

toUpperCase() converts lowercase letters to uppercase. If in a string you have mix of upper & lower case letter this function will return that string in uppercase only.

var myStr="Biswabhusan";
var ch = myStr.toUpperCase();

Output will be : BISWABHUSAN

match(regexp)

match() function accepts regular expression & returns the matches as an Array Object. Look at the example below.

var myStr="The rain in SPAIN stays mainly in the plain";
var ch=myStr.match(/ain/g);

Output will be : ain, ain, ain

replace(searchvalue, newvalue)

replace() function is one more very useful function of string operation. In-case in a string if you want to replace some characters with another characters this function helps. It accepts 2 parameters. First parameter is the search value & second parameter is the replacement for search value. Look at the example below.

var myStr="This is an awesome Blog.";
var ch = myStr.replace("awesome ","interesting ");

Output will be : This is an interesting Blog.

search(searchvalue)

search() method help to search a specific value in a string. If search value exists in the string it returns the index or else if string doesn’t contain the value it returns -1.

var myStr = "Welcome to my Blog!";
var ch = myStr.search("my");

Output will be : 11

slice(start, [end])

slice() method extract part of the string in a new string. It accepts 2 parameters start & end. Depending upon start & end value it returns the new string.

var myStr = "Visit Jquery Blog!";
var n = myStr.slice(6, 12);

Output will be : Jquery

String Function split(separator, [limit])

split() method used to divide a string from a specific character. It returns sub-strings in array. Split method accepts 2 parameters. Separator & limit. In separator if you use empty char “” it will divide all characters individually as a sub-string. Limit is always an integer value which specifies the number of splits.

var myStr = "How are you doing today?";
var arr = myStr.split(" ",3);

Output will be : How,are,you