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
- concat():
let greeting = "Hello"; let name = "Alice"; let fullGreeting = greeting.concat(", ", name, "!"); console.log(fullGreeting); // Output: "Hello, Alice!"
- charAt():
let message = "JavaScript"; console.log(message.charAt(0)); // Output: "J" console.log(message.charAt(5)); // Output: "S"
- 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)
- 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
- 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"
- substring():
let text = "Hello, world!"; console.log(text.substring(0, 5)); // Output: "Hello" console.log(text.substring(7, 12)); // Output: "world"
- 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."
- toLowerCase():
let text = "JAVASCRIPT IS AWESOME!"; console.log(text.toLowerCase()); // Output: "javascript is awesome!"
- toUpperCase():
let text = "hello, world!"; console.log(text.toUpperCase()); // Output: "HELLO, WORLD!"
- trim():
let greeting = " Hello, world! "; console.log(greeting.trim()); // Output: "Hello, world!"
- 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)
- split():
let csv = "apple,banana,cherry"; let fruits = csv.split(","); console.log(fruits); // Output: ["apple", "banana", "cherry"]
- 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
- startsWith():
let greeting = "Hello, world!"; console.log(greeting.startsWith("Hello")); // Output: true console.log(greeting.startsWith("world")); // Output: false
- endsWith():
let filename = "myfile.pdf"; console.log(filename.endsWith(".pdf")); // Output: true console.log(filename.endsWith(".txt")); // Output: false
- match():
let text = "The quick brown fox jumps."; let pattern = /quick/; console.log(text.match(pattern)); // Output: ["quick"]
- search():
let text = "The quick brown fox jumps."; let pattern = /brown/; console.log(text.search(pattern)); // Output: 10
- repeat():
let message = "Hello "; console.log(message.repeat(3)); // Output: "Hello Hello Hello "
- trimStart():
let greeting = " Hello, world! "; console.log(greeting.trimStart()); // Output: "Hello, world! "
- trimEnd():
let greeting = " Hello, world! "; console.log(greeting.trimEnd()); // Output: " Hello, world!"
- padStart():
let number = "42"; console.log(number.padStart(4, "0")); // Output: "0042"
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!"
- toString():
let num = 42; console.log(num.toString()); // Output: "42"
- valueOf():
let str = "JavaScript"; console.log(str.valueOf()); // Output: "JavaScript"
- charCodeAt():
let message = "Hello"; console.log(message.charCodeAt(0)); // Output: 72 console.log(message.charCodeAt(4)); // Output: 111
- codePointAt():
let emoji = "?"; console.log(emoji.codePointAt(0)); // Output: 128512
normalize():
let str1 = "café"; let str2 = "cafe\u0301"; console.log(str1.normalize() === str2.normalize()); // Output: true
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.
The above is the detailed content of list of the most common JavaScript string methods. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
