Home >>Javascript Tutorial >Javascript String
The strings in JavaScript is an object that represents a sequence of characters. To create strings in JavaScript, there are two methods available:
The string literal is generally created with the use of double quotes. Here is the syntax for creating string using string literal:
var stringname="string value";
Here is the example for creating string literal:
<script> var str="Hello PHPTPOINT"; document.write(str); </script>
Here is the syntax for creating string in JavaScript using new keyword:
<script> var stringname=new String("hello PHPTPOINT"); document.write(stringname); </script>
Methods | Description |
---|---|
charAt() | This method supply the char value present at the specified index. |
charCodeAt() | This method supply the Unicode value of a character present at the specified index. |
concat() | This method supply a combination of two or multiple strings. |
indexOf() | This method supply the position of a char value present in the provided string. |
lastIndexOf() | This method supply the position of a char value present in the provided string by searching a character from the very last position. |
search() | This method supply searches a specified regular expression in a provided string and returns its position only if a match is found. |
match() | This method supply searches a specified regular expression in a given string and returns that regular expression if a match is found. |
replace() | This method replaces the provided string with the specified replacement. |
substr() | This method is used to fetch the part of the provided string based on the specified starting position and length. |
substring() | This method is used to fetch the part of the provided string based on the specified index. |
slice() | This method is used to fetch the part of the provided string and allows us to assign both positive and negative index. |
toLowerCase() | This method converts the provided string into lowercase letter. |
toLocaleLowerCase() | This method converts the provided string into lowercase letter based on the host?s current locale. |
toUpperCase() | This method converts the provided string into uppercase letter. |
toLocaleUpperCase() | This method converts the provided string into uppercase letter based on the host?s current locale. |
toString() | This method supplies a string that represents a particular object. |
valueOf() | This method supplies the primitive value of string object. |
The String charAt(index) method in JavaScript returns the characters at a given index. Here is an example to simply understand this:
Example
<script> var str="PHPTPOINT"; document.write(str.charAt(2)); </script>
The String concat(str) method in JavaScript joins the two strings. Here is an example to understand this easily:
Example
<script> var s1="PHPTPOINT "; var s2="BEST TRAINING INSTITUTE"; var s3=s1.concat(s2); document.write(s3); </script>
The String indexOf(str) method in JavaScript returns the index position of the provided string. Here is an example to understand it easily:
Example
<script> var s1="PHPTPOINT is the best institute for PHP training in Noida"; var n=s1.indexOf("the"); document.write(n); </script>
The String lastIndexOf(str) Method in JavaScript returns the last index position of the provided string. Here is an example to understand it:
Example
<script> var s1=" PHPTPOINT is the best institute for PHP training in Noida "; var n=s1.lastIndexOf("the"); document.write(n); </script>
The String toLowerCase() Method in JavaScript returns the provided string in the lowercase letters. Here is an example to simply it:
Example
<script> var s1=" PHPTPOINT is the best institute for PHP training in Noida "; var s2=s1.toLowerCase(); document.write(s2); </script>
The String toUpperCase() Method in JavaScript returns the provided string in uppercase letters. Here is an example of this:
Example
<script> var s1=" phptpoint "; var s2=s1.toUpperCase(); document.write(s2); </script>
The String slice(beginIndex, endIndex) Method in JavaScript returns the string’s parts from the provided beginIndex to endIndex. Generally beginIndex is inclusive and endIndex is exclusive in Slice() method in JavaScript. Here is an example that simplify this:
Example
<script> var s1="phptpoint"; var s2=s1.slice(3,5); document.write(s2); </script>
The String trim () method in JavaScript is used to remove leading and trailing whitespaces from the string. Here is an example to easily understand this:
Example
<script> var s1=" PHP TPOINT "; var s2=s1.trim(); document.write(s2); </script>