DEV Community

Nozibul Islam
Nozibul Islam

Posted on

list of the most common JavaScript string methods

list of the most common JavaScript string methods:

  • length:
let message = "Hello, world!";
console.log(message.length); // Output: 13
Enter fullscreen mode Exit fullscreen mode
  • concat():
let greeting = "Hello";
let name = "Alice";
let fullGreeting = greeting.concat(", ", name, "!");
console.log(fullGreeting); // Output: "Hello, Alice!"
Enter fullscreen mode Exit fullscreen mode
  • charAt():
let message = "JavaScript";
console.log(message.charAt(0)); // Output: "J"
console.log(message.charAt(5)); // Output: "S"
Enter fullscreen mode Exit fullscreen mode
  • indexOf():
let sentence = "The quick brown fox jumps over the lazy dog.";
console.log(sentence.indexOf("quick")); // Output: 4
console.log(sentence.indexOf("dog")); // Output: 37
console.log(sentence.indexOf("lion")); // Output: -1 (not found)
Enter fullscreen mode Exit fullscreen mode
  • lastIndexOf():
let quote = "To be, or not to be, that is the question.";
console.log(quote.lastIndexOf("be")); // Output: 23
console.log(quote.lastIndexOf("the")); // Output: 33
Enter fullscreen mode Exit fullscreen mode
  • slice():
let fruit = "apple, banana, cherry";
console.log(fruit.slice(0, 5)); // Output: "apple"
console.log(fruit.slice(7, 13)); // Output: "banana"
console.log(fruit.slice(15)); // Output: "cherry"
Enter fullscreen mode Exit fullscreen mode
  • substring():
let text = "Hello, world!";
console.log(text.substring(0, 5)); // Output: "Hello"
console.log(text.substring(7, 12)); // Output: "world"
Enter fullscreen mode Exit fullscreen mode
  • substr():
let message = "The quick brown fox jumps.";
console.log(message.substr(4, 5)); // Output: "quick"
console.log(message.substr(10)); // Output: "brown fox jumps."
Enter fullscreen mode Exit fullscreen mode
  • toLowerCase():
let text = "JAVASCRIPT IS AWESOME!";
console.log(text.toLowerCase()); // Output: "javascript is awesome!"
Enter fullscreen mode Exit fullscreen mode
  • toUpperCase():
let text = "hello, world!";
console.log(text.toUpperCase()); // Output: "HELLO, WORLD!"
Enter fullscreen mode Exit fullscreen mode
  • trim():
let greeting = "   Hello, world!   ";
console.log(greeting.trim()); // Output: "Hello, world!"
Enter fullscreen mode Exit fullscreen mode
  • replace():
let text = "I like cats. Cats are fluffy.";
console.log(text.replace("cats", "dogs")); // Output: "I like dogs. Cats are fluffy."
console.log(text.replace(/cats/i, "dogs")); // Output: "I like dogs. Cats are fluffy." (case-insensitive)
Enter fullscreen mode Exit fullscreen mode
  • split():
let csv = "apple,banana,cherry";
let fruits = csv.split(",");
console.log(fruits); // Output: ["apple", "banana", "cherry"]
Enter fullscreen mode Exit fullscreen mode
  • includes():
let message = "The quick brown fox jumps over the lazy dog.";
console.log(message.includes("quick")); // Output: true
console.log(message.includes("tiger")); // Output: false
Enter fullscreen mode Exit fullscreen mode
  • startsWith():
let greeting = "Hello, world!";
console.log(greeting.startsWith("Hello")); // Output: true
console.log(greeting.startsWith("world")); // Output: false
Enter fullscreen mode Exit fullscreen mode
  • endsWith():
let filename = "myfile.pdf";
console.log(filename.endsWith(".pdf")); // Output: true
console.log(filename.endsWith(".txt")); // Output: false
Enter fullscreen mode Exit fullscreen mode
  • match():
let text = "The quick brown fox jumps.";
let pattern = /quick/;
console.log(text.match(pattern)); // Output: ["quick"]
Enter fullscreen mode Exit fullscreen mode
  • search():
let text = "The quick brown fox jumps.";
let pattern = /brown/;
console.log(text.search(pattern)); // Output: 10
Enter fullscreen mode Exit fullscreen mode
  • repeat():
let message = "Hello ";
console.log(message.repeat(3)); // Output: "Hello Hello Hello "
Enter fullscreen mode Exit fullscreen mode
  • trimStart():
let greeting = "   Hello, world!   ";
console.log(greeting.trimStart()); // Output: "Hello, world!   "
Enter fullscreen mode Exit fullscreen mode
  • trimEnd():
let greeting = "   Hello, world!   ";
console.log(greeting.trimEnd()); // Output: "   Hello, world!"
Enter fullscreen mode Exit fullscreen mode
  • padStart():
let number = "42";
console.log(number.padStart(4, "0")); // Output: "0042"
Enter fullscreen mode Exit fullscreen mode
  • padEnd():
    let word = "JavaScript";
    console.log(word.padEnd(15, ".")); // Output: "JavaScript....."

  • splice():

let str = "Hello, world!";
let result = str.splice(7, 5, "everyone");
console.log(result); // Output: "world"
console.log(str); // Output: "Hello, everyone!"
Enter fullscreen mode Exit fullscreen mode
  • toString():
let num = 42;
console.log(num.toString()); // Output: "42"
Enter fullscreen mode Exit fullscreen mode
  • valueOf():
let str = "JavaScript";
console.log(str.valueOf()); // Output: "JavaScript"
Enter fullscreen mode Exit fullscreen mode
  • charCodeAt():
let message = "Hello";
console.log(message.charCodeAt(0)); // Output: 72
console.log(message.charCodeAt(4)); // Output: 111
Enter fullscreen mode Exit fullscreen mode
  • codePointAt():
let emoji = "😀";
console.log(emoji.codePointAt(0)); // Output: 128512
Enter fullscreen mode Exit fullscreen mode

normalize():

let str1 = "café";
let str2 = "cafe\u0301";
console.log(str1.normalize() === str2.normalize()); // Output: true
Enter fullscreen mode Exit fullscreen mode

This covers a wide range of string manipulation tasks in JavaScript, from basic operations like getting the length or concatenating strings to more advanced techniques like pattern matching and Unicode normalization.

Top comments (0)