KandZ – Tuts

We like to help…!

JavaScript 22 🧬 string methods

a - string properties part 1
fromCharCode(...) → returns string from char codes
charAt(index) → returns the character at the specified index
charCodeAt(index) → returns the Unicode of the character at the specified index.
toString() → Converts a string object back to a primitive string
valueOf() → returns the primitive value of a string object.
toUpperCase() → converts all characters to uppercase.
toLowerCase() → converts all characters to lowercase.
concat(...values) → joins two or more strings and returns the new concatenated string.
+ Operator → also used to concatenate strings

b - string properties part 2
slice(startIndex, endIndex) → extracts a section of a string and returns it as a new string.
substring(startIndex, endIndex) → similar to slice, but doesn't handle negative indices the same way
substr(startIndex, length) → extracts a specified number of characters from a string, starting at the provided index
indexOf(searchValue, fromIndex) → returns the position of the first occurrence of a specified value.
lastIndexOf(searchValue, fromIndex) → returns the position of the last occurrence of a specified value
includes(searchValue, start) → checks if a string contains a specified substring
replace(searchValue, newValue) → replaces the first match of a specified value with another value.
split(separator, limit) → splits a string into an array of substrings based on a separator

c - string properties part 3
trim() → removes whitespace from both ends of a string.
trimStart() → removes whitespace from the start of a string (ES2019)
trimEnd() → removes whitespace from the end of a string (ES2019).
padStart(targetLength, padString) → pads the start of a string with another string to reach a specified length
padEnd(targetLength, padString) → pads the end of a string with another string to reach a specified length.
match(regexp) → searches a string for a match against a regular expression
search(regexp) → tests for a match in a string and returns the index of the match or -1 if not found.
repeat(count) → returns a new string with a specified number of copies of the original string.

Leave a Reply