Global Objects - String :
This object is a wrapper for primitive type string that helps us store text values.
It provides properties and methods to help us manipulate the given text without writing code from scratch.
The string object consists of only one property.
Let us also see some of the methods provided by the object String.
Method Name | Description | Code Snippet |
---|---|---|
charAt() | It retrieves a character which resides on the index passed as an argument | var myString = "Hello World"; console.log("Character at position 4 is : "+myString.charAt(3)); //Returns: myScript.js:2 Character at position 4 is : l |
concat() | It accepts unlimited number of string arguments, joins them and returns combined result as a new string. Note: operator ‘+’ can also be used instead. | var myString1 = "Hello"; var myString2 = " "; var myString3 = "World"; console.log("Concatenated string: "+myString1.concat(myString2,myString3)); //Returns: Concatenated string: Hello World |
indexOf() | It returns the index of the given character or may be the given set of characters in a string passed as an argument. | var myString = "Hello World"; console.log("Index of character l is : "+myString.indexOf(‘l’)); //Returns: Index of character l is : 2 |
match() | It makes use of the regular expression to look for a specific string and returns all the strings that match. | var myStr = "Are you enjoying JavaScript?"; myStr.match(‘you’); //Returns an array Index: 4 |
replace() | It accepts the substring or the regular expression as well as the string that will be used for replacement string. The idea is to replace all matches with the replacement string, and provide the modified string. | var myStr = "Are you enjoying JavaScript?"; myStr = myStr.replace(‘you’,‘they’); //Returns Are they enjoying JavaScript? |
search() | It searches for a match of regular expression in the given string and returns its position. If there is no match, it returns -1. | var myString = "can you find it?"; console.log("Occurrence of find in statement:"+myString.search(‘find’)); var myString = "Or you can not?"; console.log("Occurrence of find in statement:"+myString.search(‘find’)); //Returns Occurrence of find in statement: 8 Occurrence of find in statement: -1 |
split() | It splits the given string into the array of substrings where separator marks the index for split begin and end. For example: If the string consists of a comma(,)the given string in the argument will be split at every comma. | var myString = "Hello World"; console.log("Split string based on spaces:"+myString.split(" ")); //Returns Split of string based on spaces: Hello,World |
slice() | It extracts and returns part of string. The Second parameter is optional. If only one parameter is passed, it is the index from which string will start slicing from till end of this string. If two parameters are passed, the string between these 2 index values is sliced. Index value passed as first parameter is included whereas index value passed as second parameter is excluded. | var myString = "Hello World"; console.log("Slicing using 2 parameters: "+myString.slice(0,5)); console.log("Slicing using 1 parameter: "+myString.slice(5)); //Returns Slicing using 2 parameters: Hello Slicing using 1 parameter: World |
substring() | It extracts and returns part of string. Compared to slice() method, it can accept negative parameter,meaning slicing should start from end | var myString = "Hello World"; console.log("Substring using 2 parameters:"+myString.substring(2,5)); console.log("Substring using 1 parameter: "+myString.substring(5)); //Returns Substring using 2 parameters: llo Substring using 1 parameter: World |
substr() | It is like substring() method. Difference is, if second parameter is provided, it takes first parameter as start Index and second parameter as length for slicing string. | var myString = "Hello World"; console.log("Substr using 2 parameters: "+myString.substr(2,5)); console.log("Substr using 1 parameter: "+myString.substr(5)); //Returns Substr using 2 parameters: llo W Substr using 1 parameter: World |
toLowerCase() | Converts characters in string to lowercase | var myString = "Hello World"; console.log("Lower case string: "+myString.toLowerCase()); //Returns Lower case string: hello world |
toUpperCase() | Converts characters in string to uppercase | var myString = "Hello World"; console.log("Upper case string: "+myString.toUpperCase()); //Returns Upper case string: HELLO WORLD |
Demo :
//1. length property var myString = "Hello World"; document.write("Length of the string is : " + myString.length"); //2. method ‘charAt’ var myString = "Hello World"; document.write("Character at position 4 is : " + myString.charAt(3)"); //3. method ‘concat’ var myString1 = "Hello"; var myString2 = " "; var myString3 = "World"; document.write("concatenation of 2 strings produces this string : " + myString1.concat(myString2, myString3)); //4. method ‘indexOf’ var myString = "Hello World"; document.write("Index of character l is : " + myString.indexOf("l")"); //5. method ‘match’ var myStr = "Are you enjoying JavaScript?"; myStr.match("you");document.write("match: " + myStr"); //6. method ‘replace’ var myStr = "Are you enjoying JavaScript?"; myStr = myStr.replace("you", "they"); document.write("replaced: " + myStr"); //7. method ‘search’ var myString = "Can you find it?"; document.write("Occurrence of find in the statement : " +myString.search("find")); var myString = "Or you cannot?"; document.write("Occurrence of find in the statement : " + myString.search("find") ); //8. method ‘slice’ var myString = "Hello World"; document.write("slicing the string using 2 parameters: " + myString.slice(0, 5)); document.write("slicing the string using 1 parameter: " + myString.slice(5) ); //9. method ‘split’ var myString = "Hello World we shall stand together"; document.write("Split of string based on spaces: " + myString.split(" ")); //10. substring var myString = "Hello World"; document.write( "substring using 2 parameters: " + myString.substring(2, 5) ); document.write("substring using 1 parameter: " + myString.substring(5)"); //11. method ‘substr’ var myString = "Hello World"; document.write( "substr using 2 parameters: " + myString.substr(2, 5)"); document.write( "substr using 1 parameter: " + myString.substr(5)" ); //12. toLowerCase var myString = "Hello World"; document.write( "Lower case string: " + myString.toLowerCase()" ); //13. toUpperCase var myString = "Hello World"; document.write("Upper case string: " + myString.toUpperCase()" );
All Rights Reserved. © 2024 BookOfNetwork