Description:
Welcome to Jqueryjohn.com.
In this article, i am going to explain about
jquery string functions and their usage.
String function is used to manipulate string
objects.
1. charAt(index):
This method is used to return the character
at the specified index. The index starts from 0.
var string = "Jqueryjohn.com";
alert(string.charAt(5));
The above code will return ‘y’.
2. charCodeAt(index):
This method is used to return the Unicode
character at the specified index. The index starts from 0.
var string = "Jqueryjohn.com";
alert(string.charCodeAt(5));
The above code will return 121.
(Unicode character for ‘y’ is 121)
3. Concat(str1,str2,.. strN):
This method is used to join two or more
strings and it will not change the original string. It will return the copy the
joined values.
var string1 = "www.";
var string2 = "Jqueryjohn";
var string3 = ".com";
alert(string1.concat(string2,
string3));
The above code will return ‘www.jqueryjohn.com’.
4. indexOf(searchValue,[start]):
This method is used to find the first
occurance of the specific value in the string. If the searchvalue is not found,
it will return -1.
var string1 = "jqueryjohn";
alert(string1.indexOf('j',2));
The above code will search from the second
position and returns 6.
5. lastIndexOf(searchValue,[start]):
This method is used to find the last
occurance of the specific value in the string. If the searchvalue is not found,
it will return -1.
var string1 = "jqueryjohn";
alert(string1.lastIndexOf('j'));
The above code will return 6.
6. substr(start, count):
This method is used to extract string from
the start point to the specified count. It will not change the original string.
var string1 = "jqueryjohn";
alert(string1.substr(1,5));
The above code will return ‘query’.
7. substring(start, end):
This method is used to extract string
between the start and end places. It will not change the original string.
Difference between substr() and substring()
are, substring will not return the end character.
var string1 = "jqueryjohn";
alert(string1.substr(1,5));
The above code returns the character ‘quer’.
8. match(expression):
This method is used to searches a string for
the match against the specified expression and returns the matched values. If
no match found, it will return null.
$("#btnmatch").click(function () {
var
string1 = "jqueryjohn";
var
string2 = string1.match("john");
alert(string2);
});
The above code searches for the matches and
returns the matched values.
9. search(searchValue):
This method is used to searches a string
against the specified value and returns the position of the matched values. If
no match found, it will return -1.
$("#btnmatch").click(function () {
var
string1 = "jqueryjohn";
var
string2 = string1.match("john");
alert(string2);
});
The above code searches against the
specified value and returns ‘6’ (position of the matched values).
10. replace(searchValue,replacementValue):
replace() method used to replace the
existing values to the new values. It will not affect the original string and
it just copies the replaced values to the new variables.
var string1 = "jqueryjohn";
var string2 = string1.replace("john","john.com");
alert(string2);
The above code searches the word “john” and replace
with “john.com”. It returns
‘jqueryjohn.com’
11. Split(separator,limit):
This method used to split a string into an
array.
Split(‘’) – if we pass empty string in split
method, it will split each character into an array.
Split(‘ ‘) – if we pass blank space in split
method, it will split each individual word into an array.
var text = "www.jqueryjohn.com";
var splitText = text.split('.');
for (var
i = 0; i < splitText.length; i++) {
alert(splitText[i]);
}
The above code returns an array with three
values as www, jquery, john.
12. slice(start, end):
This method is used to extract string
between the start and end places. It will not change the original string.
var string1 = "jqueryjohn";
alert(string1.slice(1,5));
The above code returns the character ‘quer’.
13. toLowerCase():
var string1 = "JQUERYJOHN";
alert(string1.toLowerCase());
The above code returns “jqueryjohn”.
14. toUpperCase():
var string1 = "jqueryjohn";
alert(string1.toUpperCase());
The above code returns “JQUERYJOHN”.
Jquery string functions, javascript string
functions, string functions in jquery, javascript, jquery string manipulation functions, string
functions in jquery examples.
0 comments:
Post a Comment