DEV Community

Cover image for Developed by HarmonyOS: ArkTs string
程序员一鸣
程序员一鸣

Posted on

Developed by HarmonyOS: ArkTs string

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'
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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")
}
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

10. Lowercase to uppercase

the string is converted to lowercase by the toUpperCase() method.

let string = "abnerming"
let upperCase = string.toUpperCase()
console.log("==:", upperCase)
Enter fullscreen mode Exit fullscreen mode

11, uppercase to lowercase

the string is converted to lowercase by the toLowerCase() method.

let string = "ABNERMING"
let lowerCase = string.toLowerCase()
console.log("==:", lowerCase)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)