Working With Strings In JS

Working With Strings In JS: Hello everyone, welcome to the nkcoderz.com website. In this article we will going to discuss about the Working With Strings In JavaScript.

Working With Strings In JS

JavaScript provides several built-in methods for working with strings. Some of the most commonly used methods are:

  1. .length: Returns the number of characters in a string.
  2. .concat(): Concatenates two or more strings and returns a new string.
  3. .indexOf(): Returns the index of the first occurrence of a specified substring within a string, or -1 if the substring is not found.
  4. .lastIndexOf(): Returns the index of the last occurrence of a specified substring within a string, or -1 if the substring is not found.
  5. .slice(): Returns a new string that is a portion of the original string.
  6. .substring(): Returns a new string that is a portion of the original string.
  7. .substr(): Returns a new string that is a portion of the original string.
  8. .replace(): Replaces a specified substring with a new substring and returns a new string.
  9. .toLowerCase(): Returns a new string with all the characters in lower case.
  10. .toUpperCase(): Returns a new string with all the characters in upper case.
  11. .trim(): Removes whitespace from both ends of a string and returns a new string.
  12. .split(): Splits a string into an array of substrings based on a specified delimiter.
  13. .match(): Returns an array of matches for a specified regular expression.
  14. .search(): Returns the index of the first match for a specified regular expression, or -1 if no match is found.
  15. .charAt(): Returns the character at a specified index in a string.
  16. .charCodeAt(): Returns the Unicode of the character at a specified index in a string.
  17. .fromCharCode(): Returns a string created from the specified sequence of Unicode values.

Code For The Above Methods

Examples:

let str = "Hello World!";
console.log(str.length); // 12
console.log(str.concat(" How are you?")); // "Hello World! How are you?"
console.log(str.indexOf("o")); // 4
console.log(str.slice(0, 5)); // "Hello"
console.log(str.substring(0, 5)); // "Hello"
console.log(str.substr(0, 5)); // "Hello"
console.log(str.replace("World", "JavaScript")); // "Hello JavaScript!"
console.log(str.toLowerCase()); // "hello world!"
console.log(str.toUpperCase()); // "HELLO WORLD!"
console.log(str.trim()); // "Hello World!"
console.log(str.split(" ")); // ["Hello", "World!"]
console.log(str.match(/l/gi)); // ["l", "l"]
console.log(str.search(/l/gi)); // 2
console.log(str.charAt(0)); // "H"
console.log(str.charCodeAt(0)); // 72
console.log(String.fromCharCode(72)); // "H"

These are just some examples of the many methods available for working with strings in JavaScript. You can refer to the official documentation for a more comprehensive list of string methods

Conclusion

If you liked this post Working With Strings In JS, then please share this with your friends and make sure to bookmark this website for more awesome content.


If You Like This Page Then Make Sure To Follow Us on Facebook, G News and Subscribe Our YouTube Channel. We will provide you updates daily.
Share on:

NK Coderz is a Computer Science Portal. Here We’re Proving DSA, Free Courses, Leetcode Solutions, Programming Languages, Latest Tech Updates, Blog Posting Etc.

Leave a Comment