String Objects
String Method:
Example:
- The String object are the core javascript object.
- The string object is a series of characters and wraps javascript string primitive data type with a number of helper method.
- Because javascript automatically convert between string primitive and string object, we can call any of helper method of the string object on a string primitive.
- Syntax of string object: Var str=new String(string);
- The String parameter is a series of characters that has been property encoded.
Property | Description |
---|---|
constructor | Returns a reference to the String function that created the object. |
length | Returns the length of the string. |
prototype | The prototype property allows you to add properties and methods to an object. |
Method | Description |
---|---|
charAt | Returns the character at the specified index. |
charCodeAt | Returns a number indicating the Unicode value of the character at the given index. |
concat | Combines the text of two strings and returns a new string. |
indexOf | Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found. |
lastIndexOf | Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found. |
slice | Extracts a section of a string and returns a new string. |
split | Splits a String object into an array of strings by separating the string into substrings. |
toLowerCase | Returns the calling string value converted to lower case. |
toUpperCase | Returns the calling string value converted to uppercase. |
valueOf | Returns the primitive value of the specified object. |
search | Executes the search for a match between a regular expression and a specified string. |
Example:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>string object</title>
</head>
<body bgcolor="lightblue"><script type="text/javascript">
<html>
<head>
<meta charset="UTF-8">
<title>string object</title>
</head>
<body bgcolor="lightblue"><script type="text/javascript">
var str = "HELLO WORLD";
var res = str.charAt(2);
document.write(res+"<br>");
var str = "HELLO WORLD";
var res= str.charCodeAt(0);
document.write(res+"<br>" );
var str1 = "Hello ";
var str2 = "world!";
var res = str1.concat(str2);
document.write(res+"<br>" );
var str = "Hello world, welcome to my website.";
var res= str.indexOf("welcome");
document.write(res+"<br>" );
var str = "Anshul and Nitin are not best friend.";
var res= str.lastIndexOf("Nitin");
document.write(res+"<br>" );
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var res = fruits.slice(2, 4);
document.write(res+"<br>" );
var str = "How are you doing today?";
var res = str.split(" ");
document.write(res+"<br>" );
var str = "HELLO WORLD!";
var res = str.toLowerCase();
document.write(res+"<br>" );
var str = "hello world!";
var res = str.toUpperCase();
document.write(res+"<br>" );
var str = "Hello World!";
var res = str.valueOf();
document.write(res+"<br>" );
var str = "Hello mr. Komal";
var res= str.search("Komal");
document.write(res+"<br>" );
</script>
</body>
</html>
var res = str.charAt(2);
document.write(res+"<br>");
var str = "HELLO WORLD";
var res= str.charCodeAt(0);
document.write(res+"<br>" );
var str1 = "Hello ";
var str2 = "world!";
var res = str1.concat(str2);
document.write(res+"<br>" );
var str = "Hello world, welcome to my website.";
var res= str.indexOf("welcome");
document.write(res+"<br>" );
var str = "Anshul and Nitin are not best friend.";
var res= str.lastIndexOf("Nitin");
document.write(res+"<br>" );
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var res = fruits.slice(2, 4);
document.write(res+"<br>" );
var str = "How are you doing today?";
var res = str.split(" ");
document.write(res+"<br>" );
var str = "HELLO WORLD!";
var res = str.toLowerCase();
document.write(res+"<br>" );
var str = "hello world!";
var res = str.toUpperCase();
document.write(res+"<br>" );
var str = "Hello World!";
var res = str.valueOf();
document.write(res+"<br>" );
var str = "Hello mr. Komal";
var res= str.search("Komal");
document.write(res+"<br>" );
</script>
</body>
</html>