All string methods return a new string or value β they do not modify the original string.
π€ Basic Info
-
length
β Returns the length of a string.
"hello".length; // 5
π§Ύ Case Conversion
-
toUpperCase()
β Converts to uppercase. -
toLowerCase()
β Converts to lowercase.
"hello".toUpperCase(); // "HELLO"
"HELLO".toLowerCase(); // "hello"
βοΈ Extracting Parts of a String
-
slice(start, end)
β Extracts part of the string. -
substring(start, end)
β Similar toslice
but no negative index. -
substr(start, length)
(deprecated) β Extracts a substring.
"JavaScript".slice(0, 4); // "Java"
"JavaScript".substring(4, 10); // "Script"
"JavaScript".substr(4, 6); // "Script"
π Searching & Matching
-
indexOf(substring)
β First index of a match. -
lastIndexOf(substring)
β Last index of a match. -
includes(substring)
β Returns true if found. -
startsWith(substring)
β Checks beginning. -
endsWith(substring)
β Checks ending. -
search(regexp)
β Searches for match with regex. -
match(regexp)
β Returns matching string(s) with regex. -
matchAll(regexp)
β Returns all matches (ES2020+).
"hello world".includes("world"); // true
"hello world".startsWith("hello"); // true
"hello world".endsWith("world"); // true
"hello world".indexOf("o"); // 4
π§Ό Trimming Whitespace
-
trim()
β Removes whitespace from both ends. -
trimStart()
/trimLeft()
β From start. -
trimEnd()
/trimRight()
β From end.
" hello ".trim(); // "hello"
" hello ".trimStart(); // "hello "
" hello ".trimEnd(); // " hello"
π Replacing
-
replace(search, replace)
β Replace first match. -
replaceAll(search, replace)
β Replace all matches (ES2021+).
"apple banana".replace("banana", "orange"); // "apple orange"
"a-b-c".replaceAll("-", ","); // "a,b,c"
π Joining / Splitting
-
concat(string2, string3...)
β Join strings. -
split(separator)
β Split into array.
"Hello".concat(" ", "World"); // "Hello World"
"one,two,three".split(","); // ["one", "two", "three"]
π§ͺ Character Access
-
charAt(index)
β Returns character at index. -
charCodeAt(index)
β Returns Unicode of character. -
codePointAt(index)
β Returns full Unicode code point.
"Java".charAt(0); // "J"
"Java".charCodeAt(0); // 74
π String Creation & Repetition
-
repeat(n)
β Repeats string n times. -
fromCharCode(code1, code2, ...)
(Static) β Creates string from Unicode. -
fromCodePoint(code1, ...)
(Static) β Supports larger Unicode points.
"ha".repeat(3); // "hahaha"
String.fromCharCode(65, 66); // "AB"
π Comparison & Value
-
localeCompare(other)
β Compares two strings (for sorting). -
valueOf()
β Returns primitive string value. -
toString()
β Converts value to string.
"apple".localeCompare("banana"); // -1 (means "apple" < "banana")
π Summary Table
Method | Description |
---|---|
length |
Get string length |
toUpperCase() |
Convert to uppercase |
toLowerCase() |
Convert to lowercase |
slice() |
Extract part of a string |
substring() |
Extract part (no negative index) |
substr() |
Extract part (deprecated) |
indexOf() |
First index of a match |
lastIndexOf() |
Last index of a match |
includes() |
Check if contains substring |
startsWith() |
Check if starts with value |
endsWith() |
Check if ends with value |
search() |
Search using regex |
match() |
Regex match |
matchAll() |
All regex matches |
trim() |
Remove both side whitespaces |
trimStart() |
Remove start whitespace |
trimEnd() |
Remove end whitespace |
replace() |
Replace first occurrence |
replaceAll() |
Replace all occurrences |
split() |
Split string into array |
concat() |
Join strings |
charAt() |
Get character at index |
charCodeAt() |
Unicode of character |
repeat() |
Repeat string |
fromCharCode() |
Unicode to character |
localeCompare() |
Compare strings |
Top comments (0)