Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript String Methods

JS String Methods

JavaScript strings methods are used to perform various operations on string.

  • String length
  • String slice()
  • String substring()
  • String substr()
  • String replace()
  • String replaceAll()
  • String toUpperCase()
  • String toLowerCase()
  • String concat()
  • String trim()
  • String trimStart()
  • String trimEnd()
  • String padStart()
  • String padEnd()
  • String charAt()
  • String charCodeAt()
  • String split()
String Length

The length property returns the length of a string.

					 
        
          let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
          let len = text.length; // len = 26
        
      
String slice()

The slice() extracts a part of a string and returns the extracted part in a new string. The method takes 2 parameters: start position, and end position (end not included).

					 
        
          let text = "Lynxsia IT Solutions";
          let part1 = text.slice(7, 15);

          // Only start position till the rest of the string
          let part2 = text.slice(7);

          // start from the end (start not included)
          let part3 = text.slice(-7);

          // start and end from the end
          let part3 = text.slice(-7, -5);
        
      
String substring()

The substring() is similar to slice(). The difference is that start and end values less than 0 are treated as 0 in substring().

					 
        
          let text = "Lynxsia IT Solutions";
          let part = text.substring(7, 15);

          let part2 = text.substring(-7, 15); // treated as (0, 15)
        
      
String substr()

The substr() is similar to slice(). The difference is that the second parameter specifies the length of the extracted part.

					 
        
          let text = "Lynxsia IT Solutions";
          let part1 = text.substr(7, 6);

          // from 7 to rest
          let part2 = text.substr(7);

          // count from end
          let part3 = text.substr(-7);
          let part3 = text.substr(-7, 3);
        
      
String replace()

The replace() method replaces a specified value with another value in a string. It replaces only the first match.

					 
        
          let text = "Lynxsia it Solutions";
          let part = text.replace('it', 'IT');
        
      
String replaceAll()

The replaceAll() method replaces a specified value with another value in a string. It replaces all matches.

					 
        
          let text = "Lynxsia it Solutions";
          let part = text.replaceAll('it', 'IT');
        
      
String toUpperCase()

The toUpperCase() method convert string to uppercase.

					 
        
          let text = "Lynxsia it Solutions";
          let part = text.toUpperCase();
        
      
String toLowerCase()

The toLowerCase() method convert string to lowercase.

					 
        
          let text = "Lynxsia it Solutions";
          let part = text.toLowerCase();
        
      
String concat()

The concat() joins two or more strings.

					 
        
          let text1 = "Lynxsia";
          let text2 = "IT Solutions";
          let text3 = text1.concat(" ", text2);
        
      
String trim(), trimStart(), trimEnd()

The trim() method removes whitespace from both sides of a string.

The trimStart() method removes whitespace only from the start of a string.

The trimEnd() method removes whitespace only from the end of a string.

					 
        
          let text = "   Lynxsia IT Solutions   ";
          let text2 = text.trim();
          let text3 = text.trimStart();
          let text4 = text.trimEnd();
        
      
String padStart(), padEnd()

The padStart() method pads a string from the start. It pads a string with another string (multiple times) until it reaches a given length.

The padEnd() method pads a string from the end. It pads a string with another string (multiple times) until it reaches a given length.

padStart() and padEnd() methods are string methods. To pad a number, convert the number to a string first.

					 
        
          let text = "5";
          let padStart = text.padStart(4,"0");
          let padEnd = text.padStart(4,"0");
        
      
String charAt(), charCodeAt()

The charAt() method returns the character at a specified index (position) in a string.

The charCodeAt() method returns the unicode of the character at a specified index in a string.

					 
        
          let text = "5";
          let charPos = text.charAt(0);
          let charPosCode = text.charCodeAt(0);
        
      
String split()

A string can be converted to an array with the split() method.

					 
        
          text.split(",")  // Split on commas
          text.split(" ")  // Split on spaces
          text.split("|")  // Split on pipe
        
      
Property Access

ECMAScript 5 (2009) allows property access [ ] on strings. Property access might be a little unpredictable.

It makes strings look like arrays (but they are not). If no character is found, [ ] returns undefined, while charAt() returns an empty string.

It is read only. str[0] = "A" gives no error (but does not work!).

					 
        
          let text = "HELLO WORLD";
          let char = text[0];
          text[0] = "A";  // Gives no error, but does not work
        
      
All string methods return a new string. They don't modify the original string. Strings are immutable: Strings cannot be changed, only replaced.

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved