String methods in javascript

String methods in javascript

Day 5 of 30 days of javascript

In this article we will learn about the string methods in javascript

  1. charAt(index): Returns the character at the specified index in a string.

     javascriptCopy codeconst str = 'Hello';
     console.log(str.charAt(0)); // Output: "H"
    
  2. toUpperCase(): Converts a string to uppercase.

     javascriptCopy codeconst str = 'hello';
     console.log(str.toUpperCase()); // Output: "HELLO"
    
  3. toLowerCase(): Converts a string to lowercase.

     javascriptCopy codeconst str = 'HELLO';
     console.log(str.toLowerCase()); // Output: "hello"
    
  4. indexOf(searchValue): Returns the index of the first occurrence of a specified value in a string.

     javascriptCopy codeconst str = 'Hello world';
     console.log(str.indexOf('o')); // Output: 4
    
  5. slice(startIndex, endIndex): Extracts a section of a string and returns it as a new string.

     javascriptCopy codeconst str = 'Hello world';
     console.log(str.slice(6, 11)); // Output: "world"
    
  6. substring(startIndex, endIndex): Returns the substring between two indices.

     javascriptCopy codeconst str = 'Hello world';
     console.log(str.substring(6, 11)); // Output: "world"
    
  7. replace(searchValue, newValue): Searches a string for a specified value, and returns a new string where the specified value is replaced.

     javascriptCopy codeconst str = 'Hello world';
     console.log(str.replace('world', 'there')); // Output: "Hello there"
    
  8. split(separator): Splits a string into an array of substrings based on a specified separator.

     javascriptCopy codeconst str = 'apple,banana,orange';
     console.log(str.split(',')); // Output: ["apple", "banana", "orange"]
    
  9. trim(): Removes whitespace from both ends of a string.

     javascriptCopy codeconst str = '  Hello world  ';
     console.log(str.trim()); // Output: "Hello world"
    
  10. startsWith(searchString): Checks whether a string starts with the specified characters.

    javascriptCopy codeconst str = 'Hello world';
    console.log(str.startsWith('Hello')); // Output: true
    
    1. endsWith(searchString): Checks whether a string ends with the specified characters.

       javascriptCopy codeconst str = 'Hello world';
       console.log(str.endsWith('world')); // Output: true
      
    2. includes(searchString): Checks whether a string contains the specified characters.

       javascriptCopy codeconst str = 'Hello world';
       console.log(str.includes('world')); // Output: true
      
    3. concat(str1, str2, ...): Combines one or more strings into a single string.

       javascriptCopy codeconst str1 = 'Hello';
       const str2 = 'world';
       console.log(str1.concat(' ', str2)); // Output: "Hello world"
      
    4. charAt(index): Returns the character at the specified index in a string.

       javascriptCopy codeconst str = 'Hello';
       console.log(str.charAt(0)); // Output: "H"
      
    5. charCodeAt(index): Returns the Unicode value of the character at the specified index in a string.

       javascriptCopy codeconst str = 'Hello';
       console.log(str.charCodeAt(0)); // Output: 72 (Unicode value for 'H')
      
    6. repeat(count): Returns a new string with a specified number of copies of the original string.

       javascriptCopy codeconst str = 'Hello';
       console.log(str.repeat(3)); // Output: "HelloHelloHello"
      
    7. match(regexp): Searches a string for a specified pattern and returns an array of matches.

       javascriptCopy codeconst str = 'The rain in Spain falls mainly in the plain';
       const matches = str.match(/ain/g);
       console.log(matches); // Output: ["ain", "ain", "ain", "ain"]
      
    8. search(regexp): Searches a string for a specified pattern and returns the index of the first occurrence, or -1 if not found.

       javascriptCopy codeconst str = 'The rain in Spain falls mainly in the plain';
       console.log(str.search(/ain/g)); // Output: 5
      
    9. padStart(targetLength, padString): Pads the current string with another string until the resulting string reaches the given length.

       javascriptCopy codeconst str = '5';
       console.log(str.padStart(3, '0')); // Output: "005"
      
    10. padEnd(targetLength, padString): Pads the current string with another string until the resulting string reaches the given length.

      javascriptCopy codeconst str = '5';
      console.log(str.padEnd(3, '0')); // Output: "500"