HarmonyOS鸿蒙Next中string包含与替换操作
HarmonyOS鸿蒙Next中string包含与替换操作
export class weburlReplace{
replaceUrl(url:string):string{
let token:string = AppStorage.get<string>('NKToken') as string
PersistentStorage.PersistProp('StaffBean', 'noStaffBean');
let staffBean = new StaffBean(String(AppStorage.Get('StaffBean')));
if(url.search('[TOKEN]')>0){
url.replace('[TOKEN]',token)
}
if(url.search('[token]')>0){
url.replace('[token]',token)
}
if(url.search('[UID]')>0){
url.replace('[UID]',staffBean.getStaff_id())
}
if(url.search('[uid]')>0){
url.replace('[uid]',staffBean.getStaff_id())
}
return url;
}
3 回复
兄弟,可以参考下这个demo:
let ss = "http://134.176.102.68/cill/todo/index.html?token=[TOKEN]&uid=[UID]"
let s1 = ''
let s2 = ''
if (ss.includes('[TOKEN]')) {
s1 = ss.replace('[TOKEN]', '1')
}
if (ss.includes('[UID]')) {
s2 = s1.replace('[UID]', '2')
}
return s2
更多关于HarmonyOS鸿蒙Next中string包含与替换操作的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,字符串的包含与替换操作可以通过String
类提供的方法来实现。具体如下:
字符串包含操作:
- 使用
contains
方法可以判断一个字符串是否包含指定的子字符串。例如:let str = "Hello HarmonyOS"; let result = str.contains("Harmony"); console.log(result); // 输出:true
字符串替换操作:
- 使用
replace
方法可以替换字符串中的指定子字符串。例如:let str = "Hello HarmonyOS"; let newStr = str.replace("Harmony", "OpenHarmony"); console.log(newStr); // 输出:Hello OpenHarmony
这些方法在处理字符串时非常实用,可以直接在HarmonyOS应用开发中使用。
在HarmonyOS鸿蒙Next中,字符串的包含与替换操作可以通过以下方式实现:
-
包含操作:使用
includes()
方法检查字符串是否包含指定的子字符串。例如:let str = "Hello, HarmonyOS!"; let result = str.includes("HarmonyOS"); // 返回true
-
替换操作:使用
replace()
方法替换字符串中的指定子字符串。例如:let str = "Hello, HarmonyOS!"; let newStr = str.replace("HarmonyOS", "Hongmeng"); // 返回"Hello, Hongmeng!"
这些方法适用于处理字符串,帮助开发者实现高效的文本操作。