!! 测试扩展后的 string 库 !! 导入 string 库 @string; show(`=== 测试 string 库函数 ===`); !! string.upper - 转大写 text = `hello world`; show(`原字符串: `, text); show(`转大写: `, string.upper(text)); !! string.lower - 转小写 text2 = `HELLO WORLD`; show(`\n原字符串: `, text2); show(`转小写: `, string.lower(text2)); !! string.trim - 去空格 text3 = ` hello world `; show(`\n原字符串(带空格): '`, text3, `'`); show(`去空格后: '`, string.trim(text3), `'`); !! string.replace - 替换 text4 = `hello world`; show(`\n原字符串: `, text4); show(`替换 'world' 为 'oraset': `, string.replace(text4, `world`, `oraset`)); !! string.startsWith - 是否以某字符串开头 text5 = `hello world`; show(`\n原字符串: `, text5); show(`是否以 'hello' 开头: `, string.startsWith(text5, `hello`)); show(`是否以 'world' 开头: `, string.startsWith(text5, `world`)); !! string.endsWith - 是否以某字符串结尾 text6 = `hello world`; show(`\n原字符串: `, text6); show(`是否以 'world' 结尾: `, string.endsWith(text6, `world`)); show(`是否以 'hello' 结尾: `, string.endsWith(text6, `hello`)); !! string.substring - 截取子串 text7 = `hello world`; show(`\n原字符串: `, text7); show(`截取 [0:5]: `, string.substring(text7, `0`, `5`)); show(`截取 [6:11]: `, string.substring(text7, `6`, `11`)); !! string.indexOf - 查找子串位置 text8 = `hello world`; show(`\n原字符串: `, text8); show(`'world' 的位置: `, string.indexOf(text8, `world`)); show(`'test' 的位置: `, string.indexOf(text8, `test`)); !! string.length - 获取字符串长度 text9 = `hello world`; show(`\n原字符串: `, text9); show(`字符串长度: `, string.length(text9)); !! string.contains - 是否包含子串 text10 = `hello world`; show(`\n原字符串: `, text10); show(`是否包含 'world': `, string.contains(text10, `world`)); show(`是否包含 'test': `, string.contains(text10, `test`)); !! string.concat - 字符串连接 text11 = `hello`; text12 = `world`; show(`\n字符串1: `, text11); show(`字符串2: `, text12); show(`连接结果: `, string.concat(text11, text12)); !! string.repeat - 重复字符串 text13 = `ab`; show(`\n原字符串: `, text13); show(`重复3次: `, string.repeat(text13, `3`)); !! string.reverse - 反转字符串 text14 = `hello`; show(`\n原字符串: `, text14); show(`反转后: `, string.reverse(text14)); !! string.includes - 是否包含 text15 = `hello world`; show(`\n原字符串: `, text15); show(`是否包含 'world': `, string.includes(text15, `world`)); show(`是否包含 'test': `, string.includes(text15, `test`)); !! string.lastIndexOf - 最后一次出现的位置 text16 = `hello world hello`; show(`\n原字符串: `, text16); show(`'hello' 最后出现的位置: `, string.lastIndexOf(text16, `hello`)); !! string.padStart - 左侧填充 text17 = `5`; show(`\n原字符串: `, text17); show(`左侧填充到长度5(用'0'): `, string.padStart(text17, `5`, `0`)); !! string.padEnd - 右侧填充 text18 = `5`; show(`\n原字符串: `, text18); show(`右侧填充到长度5(用'*'): `, string.padEnd(text18, `5`, `*`));