...

Web Technologies - JAVASCRIPT

Back to Course

Lesson Description


Lession - #212 Javascript String Methods


String indexes start from 0. The first character is in place 0 and the second in 1 and the same follows. We can call any of the pre-defined methods for JavaScript as it automatically converts from string primitive to objects.
ex : javascript to lowercase/javascript lowercase

Different string methods are

  • charAt(indexOfCharacter>
  • charCodeAt(indexOfCharacter>
  • concat(ObjectOfString>
  • endsWith(queryString,length>
  • fromCharCode(UNICODE_NUMBER>
  • includes((queryString>
  • indexOf(queryString>
  • repeat(number>
  • replace(replaceValue,replaceWithValue>
  • search(queryString>
  • slice(startIndex,endIndex>
  • split(character>
  • startsWith(queryString>
  • toLowerCase(stringVariable>
  • length

    Let's see an example

    
    <!DOCTYPE html>
    <html>
    <body>
    
    <h2>JavaScript String Methods</h2>
    
    <p>The string methods</p>
    
    <p id="demo"></p>
    
    <script>
    let str = "Apple, Banana, Kiwi";
    var l;
    len = str.length;
    document.write(len>
    ; document.getElementById("demo">
    .innerHTML = str.slice(7>
    ; const text1 = 'hello'; const text2 = 'world'; const text3 = ' JavaScript '; // concatenating two strings const result1 = text1.concat(' ', text2>
    ; document.write("\n">
    ; document.write(result1>
    ; // "hello world" document.write("\n">
    ; // converting the text to javascript uppercase const result2 = text1.toUpperCase(>
    ; document.write(result2>
    ; // HELLO document.write("\n">
    ; // removing whitespace from the string const result3 = text3.trim(>
    ; document.write(result3>
    ; // JavaScript document.write("\n">
    Note: we can also use javascript trim (>
    to remove whitespace // converting the string to an array const result4 = text1.split(>
    ; document.write(result4>
    ; // ["hello"] document.write("\n">
    // slicing the string const result5= text1.slice(1, 3>
    ; document.write(result5>
    ; // "el" document.write("\n">
    </script> </body> </html>

    Output


    Try it here
    Can you use javascript regex match/javascript regular expression? Regular expressions are patterns used to match character combinations in strings. How can I convert javascript JSON to string? Use the JavaScript function JSON. stringify(>
    to convert it into a string.