. length : length of a string
Extracting String Characters
.charAt(position) : returns the character at a specified index (position)
.charCodeAt (position) : returns a UTF-16 code
.at (position) : returns the character at a specified index (position) in a string.( supported in all modern browsers since March 2022 )
property Access : ex) String[position]
If no character is found, [ ] returns undefined, while charAt() returns an empty string.
Extracting String Parts
.slice( start, end ) : returns the extracted part in a new string
.slice(position) : positive_ slice out the rest, negative_ slice out the end
. substring ( start, end ) : similar to .slice(), but start and end values less than 0 are treated as 0
Converting to Upper and Lower Case
.toUpperCase() : converted to upper case
.toLowerCase() : converted to lower case
.concat("asdfa","asdfasdf") : joins two or more strings
.trim() : removes whitespace from both sides
.trimStart() : removes whitespace only from the start
.trimEnd() : removes whitespace only from the end
.padStart(lenght, String ) : string (multiple times) until it reaches a given length from the start (padStart is String method so can't use number)
.padEnd(lenght, String ) : string (multiple times) until it reaches a given length from the end (padEnd is String method so can't use number)
.repeat(count) : returns a string with a number of copies
Replacing String Content
.replace( value , new value) : replaces only the first match, case sensitive
To replace case insensitive, use a regular expression with an /i flag (insensitive)
To replace all matches, use a regular expression with a /g flag
.replays(value, new value ) :
Converting a String to an Array
string can be converted to an array with the .split() method ex)x= 'j,e,o,n'; x.split(",") -> x = ['j','e','o','n']
String Search
.indexOf(value) : returns the index (position) of the first occurrence of a string in a string, or it returns -1 if the string is not found
.indexOf(value, startpoint) : if you want to select s tarting position for the search
.lastIndexOf(value) : eturns the index of the last occurrence of a specified text
.search(value) : returns the position of the match
// .indexOf(value) .search(value) . two methods are NOT equal. These are the differences
// .search() can't select start-posittion
.match( value ) : r eturns an array containing the results of matching a string against
.matchAll( value ) : returns an iterator containing the results of matching a string against a string
.includes( value ) : returns true if a string contains a specified value( case sensitive )
.startsWith( value ) : returns true if a string begins with a specified value
.startsWith( value, startposition ) : start position for the search can be specified
.endsWith( value ) : returns true if a string ends with a specified value
.endsWith( value, endposition ) : end position for the search can be specified
Interpolation
Template String provide an easy way to interpolate variables and expressions into strings
${...}
Variable Substitutions
ex )
let firstName = "John";
let lastName = "Doe";
let text = `Welcome ${firstName}, ${lastName}!`;
String Method~~~
at() | Returns an indexed character from a string |
charAt() | Returns the character at a specified index (position) |
charCodeAt() | Returns the Unicode of the character at a specified index |
codePointAt() | Returns the Unicode value at an index (position) in a string |
concat() | Returns two or more joined strings |
constructor | Returns the string's constructor function |
endsWith() | Returns if a string ends with a specified value |
fromCharCode() | Returns Unicode values as characters |
includes() | Returns if a string contains a specified value |
indexOf() | Returns the index (position) of the first occurrence of a value in a string |
lastIndexOf() | Returns the index (position) of the last occurrence of a value in a string |
length | Returns the length of a string |
localeCompare() | Compares two strings in the current locale |
match() | Searches a string for a value, or a regular expression, and returns the matches |
padEnd() | Pads a string at the end |
padStart() | Pads a string from the start |
prototype | Allows you to add properties and methods to an object |
repeat() | Returns a new string with a number of copies of a string |
replace() | Searches a string for a pattern, and returns a string where the first match is replaced |
replaceAll() | Searches a string for a pattern and returns a new string where all matches are replaced |
search() | Searches a string for a value, or regular expression, and returns the index (position) of the match |
slice() | Extracts a part of a string and returns a new string |
split() | Splits a string into an array of substrings |
startsWith() | Checks whether a string begins with specified characters |
substr() | Extracts a number of characters from a string, from a start index (position) |
substring() | Extracts characters from a string, between two specified indices (positions) |
toLocaleLowerCase() | Returns a string converted to lowercase letters, using the host's locale |
toLocaleUpperCase() | Returns a string converted to uppercase letters, using the host's locale |
toLowerCase() | Returns a string converted to lowercase letters |
toString() | Returns a string or a string object as a string |
toUpperCase() | Returns a string converted to uppercase letters |
trim() | Returns a string with removed whitespaces |
trimEnd() | Returns a string with removed whitespaces from the end |
trimStart() | Returns a string with removed whitespaces from the start |
valueOf() | Returns the primitive value of a string or a string object |
String Templates까지 함.
'JAVASCRIPT' 카테고리의 다른 글
20241122_1 Date Objects (0) | 2024.11.22 |
---|---|
20241121_2 Arrays (0) | 2024.11.21 |
20241121_1 Numbers (2) | 2024.11.21 |
20241120_2 기초2 (2) | 2024.11.20 |
20241120_1 기본 (6) | 2024.11.20 |