JavaScript - string methods

revision:


list of string methods and 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.

localeCompare() : returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

length() : returns the length of the string.

match() : used to match a regular expression against a string.

replace() : used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.

search() : executes the search for a match between a regular expression and a specified string.

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.

substr() : returns the characters in a string beginning at the specified location through the specified number of characters.

substring() : returns the characters in a string between two indexes into the string.

toLocaleLowerCase() : the characters within a string are converted to lower case while respecting the current locale.

toLocaleUpperCase() : the characters within a string are converted to upper case while respecting the current locale.

toLowerCase() : returns the calling string value converted to lower case.

toString() : returns a string representing the specified object.

toUpperCase : returns the calling string value converted to uppercase.

valueOf() : returns the primitive value of the specified object.


examples




































code:
                <div>
                    <a id="string1"></a><br>
                    <a id="string2"></a><br><br>
                    <a id="string3"></a><br>
                    <a id="string4"></a><br>
                    <a id="string5"></a><br>
                    <a id="string6"></a><br>
                    <a id="string7"></a><br>
                    <a id="string8"></a><br>
                    <a id="string9"></a><br>
                    <a id="string10"></a><br>
                    <a id="string11"></a><br>
                    <a id="string12"></a><br>
                    <a id="string13"></a><br>
                    <a id="string14"></a><br>
                    <a id="string15"></a><br>
                    <a id="string16"></a><br>
                    <a id="string17"></a><br>
                    <a id="string18"></a><br>
        
        
                </div>
                <script>
                    var str1 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry"
                    var str2 = str1.toUpperCase()
                    document.getElementById("string1").innerHTML = "first string : " + str1;
                    document.getElementById("string2").innerHTML = "second string : " + str2;
                    var newStr = str1.toUpperCase()
                    document.getElementById("string3").innerHTML = "toUpperCase : " + newStr;
                    var newStr1 = str2.toLowerCase()
                    document.getElementById("string4").innerHTML = "toLowerCase : " + newStr1;
                    var newStr2 = str1.slice(-8, -1)
                    document.getElementById("string5").innerHTML = "slice : " + newStr2;
                    var newStr3 = str1.substring(1, 6)
                    document.getElementById("string6").innerHTML = "substring : " + newStr3;
                    var newStr4 = str1.substr(8, 15)
                    document.getElementById("string7").innerHTML = "substr : " + newStr4;
                    var newStr5 = str1.replace("is", "are")
                    document.getElementById("string8").innerHTML = "replace : " + newStr5;
                    var str3 = "Java";
                    var str4 = "Script";
                    var newStr6 = str3.concat(str4)
                    document.getElementById("string9").innerHTML = "concat : " + newStr6;
                    var str5 = "              JavaScript             ";
                    var newStr7 = str5.trim()
                    document.getElementById("string10").innerHTML = "trim : " + newStr7;
                    var str6 = "JavaScript, is, useful";
                    var newStr7 = str6.split(",")
                    document.getElementById("string11").innerHTML = "split : " + newStr7[0];
                    document.getElementById("string12").innerHTML = "split : " + newStr7[2];
                    var str7 = "JavaScript is useful";
                    var newStr8 = str7.charCodeAt(str7[1]);
                    document.getElementById("string13").innerHTML = "charCodeAt : " + newStr8;
                    var newStr9 = str7.charAt(1);
                    document.getElementById("string14").innerHTML = "charAt : " + newStr9;
                    var str9 = "15"
                    var newStr10 = str9.padStart(4, "3");
                    document.getElementById("string15").innerHTML = "padStart : " + newStr10;
                    var newStr11 = str9.padEnd(4, "3");
                    document.getElementById("string16").innerHTML = "padEnd : " + newStr11;
                    var newStr12 = str6.length;
                    document.getElementById("string17").innerHTML = "length : " + newStr12;
                </script>