In the previous topic, we explored what strings are in JavaScript and how to create them. Now, let's dive into the various methods available to manipulate strings effectively. Understanding these methods is crucial for any developer working with text data.
Strings are a fundamental data type in JavaScript, and they come with a plethora of built-in methods that allow you to perform operations like searching, replacing, splitting, and transforming text. These methods can greatly simplify your code when dealing with string manipulations. In this tutorial, we'll cover some of the most commonly used string methods: length, slice, substring, replace, toUpperCase, toLowerCase, concat, trim, and split.
The length property returns the number of characters in a string.
1let str = "Hello, world!";2console.log(str.length); // Output: 13
13
The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.
1let str = "Hello, world!";2console.log(str.slice(7, 12)); // Output: "world"
world
The substring() method is similar to slice(), but it doesn't accept negative indices.
1let str = "Hello, world!";2console.log(str.substring(7, 12)); // Output: "world"
world
The replace() method replaces a specified value with another value in a string.
1let str = "Hello, world!";2console.log(str.replace("world", "universe")); // Output: "Hello, universe!"
Hello, universe!
The toUpperCase() method converts a string to uppercase letters.
1let str = "Hello, world!";2console.log(str.toUpperCase()); // Output: "HELLO, WORLD!"
HELLO, WORLD!
The toLowerCase() method converts a string to lowercase letters.
1let str = "Hello, world!";2console.log(str.toLowerCase()); // Output: "hello, world!"
hello, world!
The concat() method combines two or more strings.
1let str1 = "Hello, ";2let str2 = "world!";3console.log(str1.concat(str2)); // Output: "Hello, world!"
Hello, world!
The trim() method removes whitespace from both ends of a string.
1let str = " Hello, world! ";2console.log(str.trim()); // Output: "Hello, world!"
Hello, world!
The split() method splits a string into an array of substrings.
1let str = "Hello, world!";2console.log(str.split(", ")); // Output: ["Hello", "world!"]
["Hello", "world!"]
Let's create a simple program that takes a user input string, converts it to uppercase, and splits it into words.
1let userInput = prompt("Enter a sentence:");2let upperCaseSentence = userInput.toUpperCase();3let wordsArray = upperCaseSentence.split(" ");4console.log(wordsArray);
$ node script.jsEnter a sentence: Hello, world!["HELLO,", "WORLD!"]
| Method | Description |
|---|---|
length | Returns the number of characters in a string. |
slice() | Extracts a section of a string and returns it as a new string. |
substring() | Similar to slice(), but doesn't accept negative indices. |
replace() | Replaces a specified value with another value in a string. |
toUpperCase() | Converts a string to uppercase letters. |
toLowerCase() | Converts a string to lowercase letters. |
concat() | Combines two or more strings. |
trim() | Removes whitespace from both ends of a string. |
split() | Splits a string into an array of substrings. |
Now that you've learned about JavaScript string methods, it's time to explore another fundamental data type: numbers. In the next topic, we'll dive into how to work with numerical values in JavaScript, including arithmetic operations, number methods, and more.
Stay tuned for "JavaScript Numbers"!