 | |  |  | 本帖最后由 信息发布软件 于 2025-11-17 08:44 编辑
AIWROK软件S内置String类[String]方法小结
- /*
- 🍎交流QQ群711841924群一,苹果内测群,528816639
- 🍎🔨JavaScript字符串方法大全
- 🔨适用本文档ES5系统安卓 JavaScript引擎Rhino
- */
- // 适配打印函数:定义printl用于输出
- function printl() {
- // ES5兼容方式处理参数
- var args = Array.prototype.slice.call(arguments);
-
- // 若环境支持console.log直接调用,若不支持可替换为其他输出方式
- if (typeof console !== 'undefined' && console.log) {
- console.log.apply(console, args);
- } else {
- // 降级处理:拼接字符串输出(适配无console环境)
- var output = args.join(' ');
- if (typeof document !== 'undefined') {
- document.write(output + '<br>');
- } else if (typeof process !== 'undefined' && process.stdout) {
- // Node.js或其他环境兜底
- process.stdout.write(output + '\n');
- }
- }
- }
- // 📌charAt方法
- /*
- 类别 详情说明
- 方法功能 返回字符串中指定索引位置的字符
- 方法签名 String charAt(int index)
- 返回值 String(指定位置的字符,索引越界返回空字符串)
- 参数 - int index:字符的位置索引(从0开始)
- */
- function charAt(str, index) {
- // 参数校验:确保str是字符串,index是整数
- if (typeof str !== 'string') throw new Error('参数str必须是字符串');
- if (typeof index !== 'number' || index % 1 !== 0) throw new Error('参数index必须是整数');
- return str.charAt(index);
- }
- // 📌charCodeAt方法
- /*
- 类别 详情说明
- 方法功能 返回指定位置字符的Unicode编码
- 方法签名 Number charCodeAt(int index)
- 返回值 Number(字符的Unicode编码,索引越界返回NaN)
- 参数 - int index:字符的位置索引(从0开始)
- */
- function charCodeAt(str, index) {
- if (typeof str !== 'string') throw new Error('参数str必须是字符串');
- if (typeof index !== 'number' || index % 1 !== 0) throw new Error('参数index必须是整数');
- return str.charCodeAt(index);
- }
- // 📌indexOf方法
- /*
- 类别 详情说明
- 方法功能 正向查询子串在字符串中的位置
- 方法签名 int indexOf(String searchValue, [int fromIndex])
- 返回值 int(子串首次出现的索引,未找到返回-1)
- 参数 - String searchValue:要查询的子串(必填)
- - int fromIndex(可选):查询起始位置(默认从0开始)
- */
- function indexOf(str, searchValue, fromIndex) {
- if (typeof str !== 'string') throw new Error('参数str必须是字符串');
- if (typeof searchValue !== 'string') throw new Error('参数searchValue必须是字符串');
- if (fromIndex !== undefined) {
- return str.indexOf(searchValue, fromIndex);
- } else {
- return str.indexOf(searchValue);
- }
- }
- // 📌lastIndexOf方法
- /*
- 类别 详情说明
- 方法功能 反向查询子串在字符串中的位置
- 方法签名 int lastIndexOf(String searchValue, [int fromIndex])
- 返回值 int(子串最后出现的索引,未找到返回-1)
- 参数 - String searchValue:要查询的子串(必填)
- - int fromIndex(可选):查询起始位置(从后往前,默认字符串末尾)
- */
- function lastIndexOf(str, searchValue, fromIndex) {
- if (typeof str !== 'string') throw new Error('参数str必须是字符串');
- if (typeof searchValue !== 'string') throw new Error('参数searchValue必须是字符串');
- if (fromIndex !== undefined) {
- return str.lastIndexOf(searchValue, fromIndex);
- } else {
- return str.lastIndexOf(searchValue);
- }
- }
- // 📌length方法(封装为函数,统一调用格式)
- /*
- 类别 详情说明
- 方法功能 获取字符串的长度(包含空格、标点)
- 方法签名 int getLength(String str)
- 返回值 int(字符串的字符总数量)
- 参数 - String str:要获取长度的字符串(必填)
- */
- function getLength(str) {
- if (typeof str !== 'string') throw new Error('参数str必须是字符串');
- return str.length;
- }
- // 📌match方法
- /*
- 类别 详情说明
- 方法功能 正则表达式匹配字符串
- 方法签名 Array match(String str, RegExp regexp)
- 返回值 Array(匹配结果数组,含匹配值、索引等;未匹配返回null)
- 参数 - String str:要匹配的字符串(必填)
- - RegExp regexp:正则表达式对象(必填,支持修饰符如/g/i)
- */
- function match(str, regexp) {
- if (typeof str !== 'string') throw new Error('参数str必须是字符串');
- if (!(regexp instanceof RegExp)) throw new Error('参数regexp必须是正则表达式对象');
- return str.match(regexp);
- }
- // 📌replace方法
- /*
- 类别 详情说明
- 方法功能 替换首个匹配的子串(不修改原字符串)
- 方法签名 String replace(String str, String|RegExp searchValue, String|Function replaceValue)
- 返回值 String(替换后的新字符串)
- 参数 - String str:原始字符串(必填)
- - String|RegExp searchValue:要替换的子串或正则(必填)
- - String|Function replaceValue:替换内容(字符串或回调函数,必填)
- */
- function replace(str, searchValue, replaceValue) {
- if (typeof str !== 'string') throw new Error('参数str必须是字符串');
- return str.replace(searchValue, replaceValue);
- }
- // 📌replaceAll方法
- /*
- 类别 详情说明
- 方法功能 替换所有匹配的子串(ES2021+支持,不修改原字符串)
- 方法签名 String replaceAll(String str, String|RegExp searchValue, String|Function replaceValue)
- 返回值 String(替换后的新字符串)
- 参数 - String str:原始字符串(必填)
- - String|RegExp searchValue:要替换的子串或正则(必填)
- - String|Function replaceValue:替换内容(字符串或回调函数,必填)
- */
- function replaceAll(str, searchValue, replaceValue) {
- if (typeof str !== 'string') throw new Error('参数str必须是字符串');
- // 兼容低版本环境:若不支持replaceAll,用正则/g替代
- if (!String.prototype.replaceAll) {
- if (typeof searchValue === 'string') {
- // 字符串转正则(转义特殊字符)
- var escaped = searchValue.replace(/[.*+?^${}()|[\]\\]/g, '\\[ DISCUZ_CODE_0 ]amp;');
- return str.replace(new RegExp(escaped, 'g'), replaceValue);
- } else if (searchValue instanceof RegExp && !searchValue.global) {
- // 正则无/g修饰符,添加后匹配所有
- var flags = 'g';
- if (searchValue.ignoreCase) flags += 'i';
- if (searchValue.multiline) flags += 'm';
- var newRegexp = new RegExp(searchValue.source, flags);
- return str.replace(newRegexp, replaceValue);
- }
- }
- return str.replaceAll(searchValue, replaceValue);
- }
- // 📌split方法
- /*
- 类别 详情说明
- 方法功能 将字符串按分隔符分割为数组
- 方法签名 Array split(String str, String|RegExp separator, [int limit])
- 返回值 Array(分割后的字符串数组,分隔符不存在返回原字符串数组)
- 参数 - String str:原始字符串(必填)
- - String|RegExp separator:分隔符(必填)
- - int limit(可选):分割次数限制(默认无限制)
- */
- function split(str, separator, limit) {
- if (typeof str !== 'string') throw new Error('参数str必须是字符串');
- if (limit !== undefined) {
- return str.split(separator, limit);
- } else {
- return str.split(separator);
- }
- }
- // 📌startsWith方法
- /*
- 类别 详情说明
- 方法功能 判断字符串是否以指定子串开头
- 方法签名 Boolean startsWith(String str, String searchValue, [int position])
- 返回值 Boolean(true=是,false=否)
- 参数 - String str:原始字符串(必填)
- - String searchValue:要检测的子串(必填)
- - int position(可选):检测起始位置(默认从0开始)
- */
- function startsWith(str, searchValue, position) {
- if (typeof str !== 'string') throw new Error('参数str必须是字符串');
- if (typeof searchValue !== 'string') throw new Error('参数searchValue必须是字符串');
- if (position !== undefined) {
- // 兼容低版本环境:手动实现startsWith
- if (!String.prototype.startsWith) {
- return str.substr(position, searchValue.length) === searchValue;
- }
- return str.startsWith(searchValue, position);
- } else {
- // 兼容低版本环境:手动实现startsWith
- if (!String.prototype.startsWith) {
- return str.substr(0, searchValue.length) === searchValue;
- }
- return str.startsWith(searchValue);
- }
- }
- // 📌substr方法
- /*
- 类别 详情说明
- 方法功能 按起始索引和长度截取子串(部分环境不推荐,建议用substring)
- 方法签名 String substr(String str, int start, [int length])
- 返回值 String(截取的子串,索引越界返回空字符串)
- 参数 - String str:原始字符串(必填)
- - int start:起始索引(必填,负数表示从末尾倒数)
- - int length(可选):截取长度(默认截取到字符串末尾)
- */
- function substr(str, start, length) {
- if (typeof str !== 'string') throw new Error('参数str必须是字符串');
- if (typeof start !== 'number' || start % 1 !== 0) throw new Error('参数start必须是整数');
- if (length !== undefined) {
- return str.substr(start, length);
- } else {
- return str.substr(start);
- }
- }
- // 📌substring方法
- /*
- 类别 详情说明
- 方法功能 按索引范围截取子串(不包含结束索引)
- 方法签名 String substring(String str, int start, [int end])
- 返回值 String(截取的子串,start>end时自动交换)
- 参数 - String str:原始字符串(必填)
- - int start:起始索引(必填)
- - int end(可选):结束索引(不包含,默认截取到字符串末尾)
- */
- function substring(str, start, end) {
- if (typeof str !== 'string') throw new Error('参数str必须是字符串');
- if (typeof start !== 'number' || start % 1 !== 0) throw new Error('参数start必须是整数');
- if (end !== undefined) {
- return str.substring(start, end);
- } else {
- return str.substring(start);
- }
- }
- // 📌trim方法
- /*
- 类别 详情说明
- 方法功能 去除字符串两端的空格(含换行符\n、制表符\t)
- 方法签名 String trim(String str)
- 返回值 String(去除两端空格后的新字符串,中间空格保留)
- 参数 - String str:原始字符串(必填)
- */
- function trim(str) {
- if (typeof str !== 'string') throw new Error('参数str必须是字符串');
- // 兼容低版本环境:手动实现trim
- if (!String.prototype.trim) {
- return str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
- }
- return str.trim();
- }
- // ==============================================
- // 使用示例:所有方法的调用演示(直接运行即可看到结果)
- // ==============================================
- printl("=== 🚀 JavaScript字符串方法大全 - 使用示例 ===");
- var testStr = " Hello, JavaScript! "; // 统一测试字符串
- printl("基础测试字符串:'" + testStr + "'(含两端空格)");
- printl("---------------------------------------------\n");
- // 1. charAt示例
- printl("1. 📌 charAt方法");
- printl("调用:charAt(testStr, 7)");
- printl("结果:'" + charAt(testStr, 7) + "'(索引7的字符)\n");
- // 2. charCodeAt示例
- printl("2. 📌 charCodeAt方法");
- printl("调用:charCodeAt(testStr, 7)");
- printl("结果:" + charCodeAt(testStr, 7) + "('J'的Unicode编码)\n");
- // 3. indexOf示例
- printl("3. 📌 indexOf方法");
- printl("调用:indexOf(testStr, 'JavaScript')");
- printl("结果:" + indexOf(testStr, "JavaScript") + "(子串首次出现索引)\n");
- // 4. lastIndexOf示例
- printl("4. 📌 lastIndexOf方法");
- printl("调用:lastIndexOf(testStr, 'a')");
- printl("结果:" + lastIndexOf(testStr, "a") + "(子串最后出现索引)\n");
- // 5. getLength示例
- printl("5. 📌 getLength方法(length属性封装)");
- printl("调用:getLength(testStr)");
- printl("结果:" + getLength(testStr) + "(字符串总长度,含两端空格)\n");
- // 6. match示例
- printl("6. 📌 match方法(正则匹配)");
- printl("调用:match(testStr, /Java\\w+/)");
- var matchRes = match(testStr, /Java\w+/);
- printl("结果:" + (matchRes ? JSON.stringify(matchRes) : "null") + "(正则匹配结果数组)\n");
- // 7. replace示例
- printl("7. 📌 replace方法(替换首个匹配)");
- printl("调用:replace(testStr, 'JavaScript', 'JS')");
- printl("结果:'" + replace(testStr, "JavaScript", "JS") + "'(替换后字符串)\n");
- // 8. replaceAll示例
- printl("8. 📌 replaceAll方法(替换所有匹配)");
- printl("调用:replaceAll(testStr, 'l', 'L')");
- printl("结果:'" + replaceAll(testStr, "l", "L") + "'(所有'l'替换为'L')\n");
- // 9. split示例
- printl("9. 📌 split方法(字符串分割)");
- printl("调用:split(testStr, ', ')");
- printl("结果:" + JSON.stringify(split(testStr, ", ")) + "(分割后的数组)\n");
- // 10. startsWith示例
- printl("10. 📌 startsWith方法(判断开头)");
- printl("调用:startsWith(testStr, ' Hello')");
- printl("结果:" + startsWith(testStr, " Hello") + "(是否以' Hello'开头)\n");
- // 11. substr示例
- printl("11. 📌 substr方法(按长度截取)");
- printl("调用:substr(testStr, 7, 10)");
- printl("结果:'" + substr(testStr, 7, 10) + "'(从索引7截取10个字符)\n");
- // 12. substring示例
- printl("12. 📌 substring方法(按索引范围截取)");
- printl("调用:substring(testStr, 7, 17)");
- printl("结果:'" + substring(testStr, 7, 17) + "'(从索引7到17截取,不含17)\n");
- // 13. trim示例
- printl("13. 📌 trim方法(去除两端空格)");
- printl("调用:trim(testStr)");
- printl("结果:'" + trim(testStr) + "'(去除两端空格后的纯净字符串)\n");
- printl("=== ✅ 所有字符串方法示例运行完毕 ===");
复制代码 [color=var(--md-box-h3-text-color,var(--md-box-global-text-color))]方法 1:charAt项目 | 说明 | 方法功能 | 返回指定位置的字符串 | 方法签名 | String charAt(int arg0) | 返回值 | String | 参数 | int arg0: 位置 | 案例 | String.charAt(0) | [color=var(--md-box-h3-text-color,var(--md-box-global-text-color))]方法 2:charCodeAt项目 | 说明 | 方法功能 | 返回指定字符的 Unicode 编码 | 方法签名 | String charCodeAt(int arg0) | 返回值 | String | 参数 | int arg0: 位置 | 案例 | String.charCodeAt(0) | [color=var(--md-box-h3-text-color,var(--md-box-global-text-color))]方法 3:indexOf项目 | 说明 | 方法功能 | 查询字符串位置 | 方法签名 | int indexOf(String arg0) | 返回值 | int | 参数 | String arg0: 查询字符 | 案例 | String.indexOf("") | [color=var(--md-box-h3-text-color,var(--md-box-global-text-color))]方法 4:lastIndexOf项目 | 说明 | 方法功能 | 反向查询字符串位置 | 方法签名 | String lastIndexOf(int arg0) | 返回值 | String | 参数 | int arg0: 位置 | 案例 | String.lastIndexOf(0) | [color=var(--md-box-h3-text-color,var(--md-box-global-text-color))]方法 5:length项目 | 说明 | 方法功能 | 字符串长度 | 方法签名 | String length(int arg0) | 返回值 | String | 参数 | int arg0: 位置 | 案例 | String.length(0) | [color=var(--md-box-h3-text-color,var(--md-box-global-text-color))]方法 6:match项目 | 说明 | 方法功能 | 正则匹配 | 方法签名 | String match(int arg0) | 返回值 | String | 参数 | int arg0: 位置 | 案例 | String.match(0) | [color=var(--md-box-h3-text-color,var(--md-box-global-text-color))]方法 7:replace项目 | 说明 | 方法功能 | 字符替换一次 | 方法签名 | String replace(int arg0) | 返回值 | String | 参数 | int arg0: 位置 | 案例 | String.replace(0) | [color=var(--md-box-h3-text-color,var(--md-box-global-text-color))]方法 8:replaceAll项目 | 说明 | 方法功能 | 字符替换全部 | 方法签名 | String replaceAll(int arg0) | 返回值 | String | 参数 | int arg0: 位置 | 案例 | String.replaceAll(0) | [color=var(--md-box-h3-text-color,var(--md-box-global-text-color))]方法 9:split项目 | 说明 | 方法功能 | 字符分割 | 方法签名 | String split(int arg0) | 返回值 | String | 参数 | int arg0: 位置 | 案例 | String.split(0) | [color=var(--md-box-h3-text-color,var(--md-box-global-text-color))]方法 10:startsWith项目 | 说明 | 方法功能 | 是否以字符开头 | 方法签名 | String startsWith(int arg0) | 返回值 | String | 参数 | int arg0: 位置 | 案例 | String.startsWith(0) | [color=var(--md-box-h3-text-color,var(--md-box-global-text-color))]方法 11:substr项目 | 说明 | 方法功能 | 截取指定长度字符 | 方法签名 | String substr(int arg0) | 返回值 | String | 参数 | int arg0: 位置 | 案例 | String.substr(0) | [color=var(--md-box-h3-text-color,var(--md-box-global-text-color))]方法 12:substring项目 | 说明 | 方法功能 | 截取两索引之间字符 | 方法签名 | String substring(int arg0) | 返回值 | String | 参数 | int arg0: 位置 | 案例 | String.substring(0) | [color=var(--md-box-h3-text-color,var(--md-box-global-text-color))]方法 13:trim项目 | 说明 | 方法功能 | 去除两端空格 | 方法签名 | String trim(int arg0) | 返回值 | String | 参数 | int arg0: 位置 | 案例 | String.trim(0) |
| |  | |  |
|