HarmonyOs DevEco Studio小技巧8--string的用法

目录

一、声明与初始化

二、字符串操作

1、拼接字符串

+:可以使用+运算符来拼接字符串,例如:

[concat(string1, string2, ...):返回连接后的字符串](#concat(string1, string2, ...):返回连接后的字符串)

2、返回字符串索引

[indexOf(searchValue, fromIndex):返回子字符串首次出现的索引,找不到返回-1,从左至右查找。](#indexOf(searchValue[, fromIndex]):返回子字符串首次出现的索引,找不到返回-1,从左至右查找。)

[lastIndexOf(searchValue, fromIndex):返回子字符串首次出现的索引,找不到返回-1。从右至左查找。](#lastIndexOf(searchValue[, fromIndex]):返回子字符串首次出现的索引,找不到返回-1。从右至左查找。)

search():找到与正则表达式匹配的第一个子串的索引,如果没有找到则返回-1:

3、返回字符串

charAt(index):返回指定字符串

[replace(regexp|substr, newSubstr|function):替换匹配项并返回新字符串。](#replace(regexp|substr, newSubstr|function):替换匹配项并返回新字符串。)

[slice(start, end)与substring(start, end):返回指定区间内的子字符串。](#slice(start[, end])与substring(start[, end]):返回指定区间内的子字符串。)

4、返回字符串数组

[split(separator, limit):使用指定分隔符将字符串分割成数组。](#split(separator[, limit]):使用指定分隔符将字符串分割成数组。)

match():使用正则表达式搜索字符串,并返回匹配项数组

matchAll():返回一个迭代器,其包含了所有匹配正则表达式的捕获组

5、检查字符串

includes():检查字符串是否包含某个子字符串

[startsWith() 和 endsWith():检查字符串是否以某个子字符串开始或结束](#startsWith() 和 endsWith():检查字符串是否以某个子字符串开始或结束)

6、其他

toLowerCase()与toUpperCase():转换为小写和转换为大写

trim():移除字符串两侧的空白字符。

repeat():重复字符串指定次数

[padStart() 和 padEnd():在字符串开始或结束添加指定字符以达到固定长度](#padStart() 和 padEnd():在字符串开始或结束添加指定字符以达到固定长度)

三、字符串与其他数据类型的转换

1、转换为数字

2、其他数据类型转换为字符串


在HarmonyOS中,string(字符串)有以下常见用法:

一、声明与初始化

  • 使用单引号或双引号来声明字符串
TypeScript 复制代码
let str1 = 'Hello'; //单引号
let str2 = "World"; //双引号
  • 也可以使用反引号(模板字符串)进行声明,并且可以在其中嵌入表达式,如:
TypeScript 复制代码
let name = 'John'; 
let greeting = `Hello, ${name}!`;//反引号

**反引号 `**一般在那个一排数字那里(为啥有这个,因为我第一次也没找着)

二、字符串操作

1、拼接字符串

+:可以使用+运算符来拼接字符串,例如:
TypeScript 复制代码
let str1 = 'Hello'; //单引号
let str2 = "World"; //双引号
let str3 = str1 + " " + str2;
console.log(let) //Hello World
**concat(string1, string2, ...):**返回连接后的字符串
TypeScript 复制代码
let str1 = "Hello"; 
let str2 = " ";
let str3 = "world";
let result = str1.concat(str2, str3);
console.log(result); // 输出 "Hello world"

2、返回字符串索引

indexOf(searchValue, fromIndex):返回子字符串首次出现的索引,找不到返回-1,从左至右查找。
TypeScript 复制代码
let str = "hello world";
console.log(str.indexOf("orld")); // 输出 7
lastIndexOf(searchValue, fromIndex):返回子字符串首次出现的索引,找不到返回-1。从右至左查找。
TypeScript 复制代码
let str = "hello world";
console.log(str.lastIndexOf("l")); // 输出 9
**search():**找到与正则表达式匹配的第一个子串的索引,如果没有找到则返回-1:
TypeScript 复制代码
let text = "Can you find me?";
let pos = text.search(/find/i);
console.log(pos); // 输出 7

3、返回字符串

charAt(index):返回指定字符串
TypeScript 复制代码
let str = "Hello";
console.log(str.charAt(1)); // 输出 "e"
replace(regexp|substr, newSubstr|function):替换匹配项并返回新字符串。
TypeScript 复制代码
 let str = "I love node.js";
 let newStr = str.replace("node.js", "js");
 console.log(newStr); // I love js
slice(start, end)与substring(start, end):返回指定区间内的子字符串。
TypeScript 复制代码
str = "Hello, World!";
console.log(str.substring(0, 5)); // 输出 "Hello"
console.log(str.substring(7)); // 输出 "World!"
console.log(str.substring(-1)); // 输出 "Hello, World!",因为 -1 被当作 0
console.log(str.substring(10, 7)); // 输出 "Hello",因为参数被交换了
console.log(str.slice(0, 5)); // 输出 "Hello"
console.log(str.slice(7)); // 输出 "World!"
console.log(str.slice(-1)); // 输出 "!",因为从字符串的尾部开始计数
console.log(str.slice(10, 7)); // 输出 "",因为 startIndex 大于 endIndex

4、返回字符串数组

split(separator, limit):使用指定分隔符将字符串分割成数组。
TypeScript 复制代码
let str = "apple,banana,grape";
let fruits = str.split(",");
console.log(fruits); // 输出 ["apple", "banana", "grape"]
**match():**使用正则表达式搜索字符串,并返回匹配项数组
TypeScript 复制代码
let text = "cat, bat, sat, fat";
let pattern = /.at/;
let matches = text.match(pattern);
console.log(matches); // 输出 ["cat"]
**matchAll():**返回一个迭代器,其包含了所有匹配正则表达式的捕获组
TypeScript 复制代码
let text = "cat, bat, sat, fat";
let regex = /.at/g;
for (const match of text.matchAll(regex)) {
     console.log(match[0]);
   }
 // 输出 "cat", "bat", "sat", "fat"

5、检查字符串

**includes():**检查字符串是否包含某个子字符串
TypeScript 复制代码
let str = "Hello, world!";
console.log(str.includes("world")); // 输出 true
startsWith() 和 **endsWith():**检查字符串是否以某个子字符串开始或结束
TypeScript 复制代码
let url = "https://ljynet.com";
console.log(url.startsWith("https")); // 输出 true
console.log(url.endsWith(".cn")); // 输出 false

6、其他

toLowerCase()与toUpperCase():转换为小写和转换为大写
TypeScript 复制代码
let str = "APPLE";
console.log(str.toLowerCase()); // 输出 "apple"
let str2 = "apple";
console.log(str.toUpperCase()); // 输出 "APPLE"
trim():移除字符串两侧的空白字符。
TypeScript 复制代码
let str = "   hello   ";
console.log(str.trim()); // 输出 "hello"
console.log(str.trimStart()); // 输出 "hello   "
console.log(str.trimEnd()); // 输出 "   hello"
**repeat():**重复字符串指定次数
TypeScript 复制代码
let str = "https://www.baidu.com";
console.log(str.repeat(3)); //https://www.baidu.com://www.baidu.com://www.baidu.com
padStart() 和 **padEnd():**在字符串开始或结束添加指定字符以达到固定长度
TypeScript 复制代码
let num = "123";
console.log(num.padStart(5, "0")); // 输出 "00123"
console.log(num.padEnd(5, "!")); // 输出 "123!!!"

三、字符串与其他数据类型的转换

1、转换为数字

  • parseInt():将字符串转换为整数
TypeScript 复制代码
let num1 = parseInt('123'); 
  • **parseFloat():**转换为浮点数函数
TypeScript 复制代码
let num2 = parseFloat('3.14')

2、其他数据类型转换为字符串

  • toString():(大多数数据类型都有这个方法)
TypeScript 复制代码
let num = 123; let strNum = num.toString();
  • 直接将其他数据类型与空字符串拼接。
TypeScript 复制代码
let strNum2 = "" + num;
相关推荐
小魔女千千鱼4 小时前
把 Go 塞进鸿蒙PC:windows上用 c-shared 跑 2048
harmonyos
TrisighT4 小时前
Electron 跑在鸿蒙 PC 上,单窗口和多窗口内存差 800MB?我抓了 5 组数据
性能优化·electron·harmonyos
TrisighT1 天前
AI写埋点代码,35%覆盖率坑惨运营
harmonyos·arkts·arkui
Junerver4 天前
把 DevEco Code 的 HarmonyOS 开发能力装进口袋——harmonyos-dev-skill
harmonyos
LDR0065 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术5 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
程序猿追5 天前
那个右下角的小数字怎么“卡”住我打字——我用 HarmonyOS 自己写了一个字数限制输入框
pytorch·华为·harmonyos
古德new5 天前
鸿蒙PC使用electron迁移:Joplin Electron 桌面适配全记录
华为·electron·harmonyos
码云数智-园园5 天前
C++20 Modules 模块详解
java·开发语言·spring