Comprehensive Guide to JavaScript String Methods
JavaScript String Methods Overview of String Methods In JavaScript, strings are primitive and immutable. This means that whenever you use a string method, it generates a new str...
JavaScript String Methods
Overview of String Methods
In JavaScript, strings are primitive and immutable. This means that whenever you use a string method, it generates a new string rather than changing the original one. Here are some basic string methods you should know:
String lengthString charAt()String charCodeAt()String codePointAt()String concat()String at()String [ ]String slice()String substring()String substr()String toUpperCase()String toLowerCase()String isWellFormed()String toWellFormed()String trim()String trimStart()String trimEnd()String padStart()String padEnd()String repeat()String replace()String replaceAll()String split()
Basic Operations
JavaScript String Length
The length property provides the number of characters in a string.
Extracting Characters
JavaScript offers four primary methods to extract characters from strings:
at(position)charAt(position)charCodeAt(position)- Using property access like arrays
[ ]
Detailed String Methods
charAt()
The charAt() method retrieves the character at a specific index in a string.
charCodeAt()
This method returns the UTF-16 code of the character at a given index, ranging from 0 to 65535.
codePointAt()
Retrieves the code point value at a specific index.
at()
Introduced in ES2022, the at() method fetches the character at a specified index and supports negative indexing, unlike charAt().
Property Access [ ]
Using property access can be unpredictable:
- Strings appear like arrays, but they are not.
- If no character is found,
[ ]returnsundefined, whilecharAt()returns an empty string. - It is read-only. For example,
str[0] = "A"does not throw an error, but it won't work.
Combining Strings
concat()
The concat() method combines two or more strings and is an alternative to the plus operator. All string methods return a new string, affirming that strings are immutable and cannot be changed, only replaced.
Extracting String Parts
Methods for extracting parts of a string include:
slice(start, end)substring(start, end)substr(start, length)
slice()
Extracts a section of a string and returns it as a new string. It requires start and end positions, where the end is not included.
substring()
Similar to slice(), but negative values for start and end are treated as 0. Omitting the second parameter slices the rest of the string.
substr()
Removed in newer JavaScript standards, this method is similar to slice(), but the second parameter specifies the length of the substring.
Changing Case
toUpperCase(): Converts a string to upper case.toLowerCase(): Converts a string to lower case.
String Well-Formedness
isWellFormed()
Checks if a string is well-formed and returns true or false.
toWellFormed()
Returns a new string where lone surrogates are replaced with the Unicode replacement character.
Trimming Strings
trim()
Removes whitespace from both ends of a string.
trimStart()
Removes whitespace from the start of a string. Supported in modern browsers since January 2020.
trimEnd()
Removes whitespace from the end of a string. Supported in modern browsers since January 2020.
Padding Strings
padStart()
Pads a string from the beginning until a certain length is reached. Convert a number to a string before using.
padEnd()
Pads a string from the end until it reaches a specified length. Convert a number to a string before using.
Repeating and Replacing
repeat()
Creates a new string by repeating the original string a specified number of times.
replace()
Replaces a specified value with another in a string. By default, it replaces only the first match. For all matches, use a regular expression with the /g flag.
replaceAll()
Introduced in 2021, this method replaces all occurrences of a substring or regex pattern. It requires a global flag if a regex is used.
Converting Strings to Arrays
split()
Converts a string into an array. Omitting the separator will return an array with the whole string as its first element. Using an empty string as the separator results in an array of individual characters.
Complete Reference
For an in-depth understanding of JavaScript properties and methods, including full descriptions and examples, consult a comprehensive JavaScript reference.