Foreword
this article code case based on api13.
Strings are still very important in actual development, and there are many usage involved, such as search, replacement, cutting and so on in strings.
A String is a finite sequence of zero or more characters and is widely used in computer programming and data processing. A string can contain letters, numbers, punctuation, spaces, or even an empty string (that is, a string that contains no characters). Strings are the basic representation of text information, and in almost all programming languages there are specialized string data types or classes to handle them.
Representation method
in actual development, you can use single quotation marks (') or double quotation marks (") to indicate that both methods are possible, as shown in the following code:
let string = "I am a double quoted string"
let string2 = 'I am a single quoted string'
if it is a member variable declaration, the keyword prefix may not be used:
string = "I am a double quoted string"
string2 = 'I am a single quoted string'
main methods
methods/Properties | overview |
---|---|
length | returns the length of a string |
indexOf() | returns the position of the first occurrence of a specified string value in a string. |
lastIndexOf() | searches the string from the back to the front, and calculates the position of the last occurrence of the returned string from the starting position (0). |
charAt() | returns the character at the specified position. |
split() | Splits a string into an array of substrings. |
substring() | extracts the characters between two specified index numbers in a string. |
replace() | replace substrings that match regular expressions |
search() | retrieve a value that matches a regular expression |
introduction of each method
1. Obtain the length
the length of the string is returned through the built-in length attribute, as follows:
let string = "I am a double quoted string"
let length = string.length
console.log("length:" + length)
2. Obtain location
returns a specified string value in a string by using indexOf() for the first time the position of appearance must be remembered as the first time, that is, the position of the first appearance.
let string = "Programmer Yiming is a top performer among Hongmeng developers"
let index = string.indexOf("a")
console.log("a:" + index)
3. Whether there is a character
given a string, determine whether the string exists (contains) a character or string, is also determined by the indexOf method, the existence is not -1, does not exist is -1.
let string = "Programmer Yiming is a top performer among Hongmeng developers"
if (string.indexOf("harmony") != -1) {
console.log("===yes")
} else {
console.log("===no")
}
4. Get the position from the back
the indexOf() method searches from the front, while lastIndexOf() searches for the string from the back to the front, and calculates the position of the last occurrence of the returned string from the starting position (0).
let string = "Programmer Yiming is a top performer among Hongmeng developers"
let index = string.lastIndexOf("==")
console.log("==" + index)
5. Return the character in the specified position
charAt() returns the character at the specified position.
let string = "Programmer Yiming is a top performer among Hongmeng developers"
let char = string.charAt(3)
console.log("==" + char)
6. String segmentation
split the string into an array of substrings through split().
let string = "Programmer Yiming is a top performer among Hongmeng developers"
let sArray = string.split("a")
console.log("===", sArray)
In addition to the common segmentation above, you can also control the return array length.
let string = "Programmer Yiming is a top performer among Hongmeng developers"
let sArray = string.split(" ", 3)
console.log("===", sArray)
7. Extract characters
extracts the character between two specified index numbers in the string through substring().
let string = "Programmer Yiming is a top performer among Hongmeng developers"
let eString = string.substring(0, 6)
console.log("==", eString)
If there is only one parameter, then it is from the set index to the end of the string.
let string = "Programmer Yiming is a top performer among Hongmeng developers"
let eString = string.substring(3)
console.log("==", eString)
8. Replace characters
replace the substring that matches the regular expression with replace().
let string = "Programmer Yiming is a top performer among Hongmeng developers"
let rString = string.replace("Yiming","AbnerMing")
console.log("==", rString)
Of course, you can also use regular replacement, such as replacing all the numbers in a string with "good" words.
let re = /\d+/g;
let string = "Programmer Yiming 6 is one of the top 9 developers among 7 HarmonyOS developers"
let rString = string.replace(re,"one")
console.log("==", rString)
9. Retrieving Characters
to determine whether a string exists, you can use the indexOf method to determine, as described in the third method introduction above, in addition to the indexOf method, the system also provides a search() method for retrieval, not -1 exists, -1 does not exist.
let string = "Programmer Yiming is a top performer among Hongmeng developers"
let isSearch = string.search("is") != -1
console.log("==", isSearch)
10. Lowercase to uppercase
the string is converted to lowercase by the toUpperCase() method.
let string = "abnerming"
let upperCase = string.toUpperCase()
console.log("==:", upperCase)
11, uppercase to lowercase
the string is converted to lowercase by the toLowerCase() method.
let string = "ABNERMING"
let lowerCase = string.toLowerCase()
console.log("==:", lowerCase)
12. Return the Unicode encoding of the specified location
through charCodeAt(), returns the Unicode encoding of the character at the specified position.
let string = "Programmer Yiming is a top performer among Hongmeng developers"
let char = string.charCodeAt(3)
console.log("==" + char)
Summary
String type is a very important data type in development. In addition to the above method overview, there are other uses such as String object and regular expression. We will talk about it in a later chapter.
Top comments (0)