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

目录

一、声明与初始化

二、字符串操作

1、拼接字符串

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

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

2、返回字符串索引

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

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

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

3、返回字符串 

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

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

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

4、返回字符串数组

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

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

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

5、检查字符串 

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

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

 6、其他

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

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

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

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

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

1、转换为数字

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


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

一、声明与初始化

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

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

二、字符串操作

1、拼接字符串

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

2、返回字符串索引

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

3、返回字符串 

  • charAt(index):返回指定字符串
let str = "Hello";
console.log(str.charAt(1)); // 输出 "e"
  • replace(regexp|substr, newSubstr|function):替换匹配项并返回新字符串。
 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]):返回指定区间内的子字符串。
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]):使用指定分隔符将字符串分割成数组。
let str = "apple,banana,grape";
let fruits = str.split(",");
console.log(fruits); // 输出 ["apple", "banana", "grape"]
  •  match():使用正则表达式搜索字符串,并返回匹配项数组
let text = "cat, bat, sat, fat";
let pattern = /.at/;
let matches = text.match(pattern);
console.log(matches); // 输出 ["cat"]
  • matchAll():返回一个迭代器,其包含了所有匹配正则表达式的捕获组
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():检查字符串是否包含某个子字符串
let str = "Hello, world!";
console.log(str.includes("world")); // 输出 true
  • startsWith() 和 endsWith():检查字符串是否以某个子字符串开始或结束
let url = "https://ljynet.com";
console.log(url.startsWith("https")); // 输出 true
console.log(url.endsWith(".cn")); // 输出 false

 6、其他

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

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

1、转换为数字

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

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

  • toString():(大多数数据类型都有这个方法)
let num = 123; let strNum = num.toString();
  • 直接将其他数据类型与空字符串拼接。
let strNum2 = "" + num;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值