JavaScript - string methods

revision:


list of string methods and description

charAt() : returns the character at the specified index.

syntax:
        let str = "Hello";
        console.log(str.charAt(1)); // 'e'
    

charCodeAt() : returns a number indicating the Unicode value of the character at the given index.

syntax:
        let str = "A";
        console.log(str.charCodeAt(0)); // 65
    

concat() : combines the text of two strings and returns a new string.

syntax:
        let str1 = "Hello";
        let str2 = "World";
        console.log(str1.concat(" ", str2)); // "Hello World"
    

indexOf() : returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.

syntax:
        let str = "Hello World";
        console.log(str.indexOf("o")); // 4
    

lastIndexOf() : returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.

syntax:
        let str = "Hello World";
        console.log(str.lastIndexOf("o")); // 7
    

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

syntax:
        console.log("a".localeCompare("b")); // -1
        console.log("b".localeCompare("a")); // 1
        console.log("a".localeCompare("a")); // 0
    

length() : returns the length of the string.

syntax:
        let str = "Hello";
        console.log(str.length); // 5
    

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

syntax:
        let str = "The rain in Spain falls mainly in the plain";
        let matches = str.match(/in/g);
        console.log(matches); // ["in", "in", "in", "in"]
    

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

syntax:
        let str = "Hello World";
        console.log(str.replace("World", "Universe")); // "Hello Universe"
    

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

syntax: 

slice() : extracts a section of a string and returns a new string.

syntax:
        let str = "Hello World";
        console.log(str.slice(0, 5)); // "Hello"
    

split() : splits a String object into an array of strings by separating the string into substrings.

syntax:
        let str = "apple,banana,orange";
        console.log(str.split(",")); // ["apple", "banana", "orange"]
    

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

syntax:
        let str = "Hello World";
        console.log(str.substr(6, 5)); // "World"
    

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

syntax:
        let str = "Hello World";
        console.log(str.substring(0, 5)); // "Hello"
    

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

syntax:
        const dotted = "İstanbul";
        console.log(`EN-US: ${dotted.toLocaleLowerCase("en-US")}`);  // Expected output: "i̇stanbul"
        console.log(`TR: ${dotted.toLocaleLowerCase("tr")}`); // Expected output: "istanbul"
    

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

syntax:
        const city = "istanbul";
        console.log(city.toLocaleUpperCase("en-US")); // Expected output: "ISTANBUL"
        console.log(city.toLocaleUpperCase("TR"));  // Expected output: "İSTANBUL"

    

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

syntax:
        let text = "Hello World!";
        let result = text.toLowerCase();
        console.log(result); // "hello world!"
    

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

syntax:
        let text = "Hello World!";
        let result = text.toString();
        console.log(result); // "Hello World!"
    

toUpperCase() : returns the calling string value converted to uppercase.

syntax:
         let str = "hello";
        console.log(str.toUpperCase()); // "HELLO"
    

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

syntax:
        let num = new Number(12);
        console.log(num);  // Output: [Number: 12]
        console.log(num.valueOf()); // Output: 12
    

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>