# JavaScript String Methods
● String Methods and Properties
Primitive values, like "Komorebi", can not have properties or methods because they are not objects.
But with JavaScript, methods and properties are also available to primitive values, because JavaSCript treats primitive values as objects when executing methods and properties.
● String Length
The length property returns the length of a string.
● Finding a String in a String
The indexOf() method returns the index of the first occurrence of a specified text in a string.
The lastIndexOf() method returns the index of the last occurrence of a specified text in a string.
Both the indexOf(), and the lastIndexOf() methods return -1 if the text is not found.
Both methods accept a second parameter as the starting position for the search.
● Searching for a String in a String
The search() method searches a string for a specified value and returns the position of the match.
The two methods, indexOf() and search(), are equal.
They accept the same arguments, and they return the same value.
The two methods are equal, but the search() method can take much more powerful search values.
● Extracting String Parts.
There are 3 methods for extracting a part of a string.
- slice(start, end)
- substring(start, end)
- substr(start, length)
● The slice() Method
slice() extracts a part of a string and returns the extracted part in a new string.
The method takes 2 parameters. the starting index, and the ending index.
This example slices out a portion of a string from position 4 to position 10.
This example slices out a portion of a string from position -18 to position -9
When I omit the second parameter, the method will slice out the rest of the string.
● The substring() Method
substring() is similar to slice().
The difference is that substring() cannot accept negative indexes.
When I omit the second parameter, substring() will slice out the rest of the string.
● The substr() Method
substr() is similar to slice().
The difference is that the second parameter specifies the length of the extracted part.
● Replacing String Content
The replace() method replaces a specified value with another value in a string.
The replace() method can also take a regular expression as the search value.
By default, the replace() function replaces only the first match. To replace all matches, use a regular expression with a g flag ( For global match).
● Converting to Upper and Lower Case
A string is converted to upper case with toUpperCase().
A string is converted to lower case with toLowerCase().
● The concat() Method
concat() joins two or more strings.
The concat() method can be used instead of the plus operator.
● Extracting String Characters
There are 2 safe methods for extracting string characters.
- charAt(position)
- charCodeAt(position)
● The charAt() Method
The charAt() method returns the character at a specified index in a string.
● The charCodeAt() Method
The charCodeAt() method returns the unicode of the character at a specified index in a string.
● Converting a String to an Array
A string can be converted to an array with the split() method.
If the separator is omitted, the returned array will contain the whole string in index[0].
If the separator is "", the returned array will be an array of single characters.
If the separator is omitted, the returned array will contain the whole string in index[0].
If the separator is "", the returned array will be an array of single characters.
No comments:
Post a Comment