365Tools
发布时间:2024-04-12 13:30:01
JavaScript String 对象用于处理字符串,其中提供了大量操作字符串的方法,以及一些属性。
var val = new String(value);
var val = String(value);
| 属性 | 描述 |
|---|---|
| constructor | 获取创建此对象的 String() 函数的引用 |
| length | 获取字符串的长度 |
| prototype | 通过该属性您可以向对象中添加属性和方法 |
var str = new String('JavaScript');
String.prototype.name = null;
str.name = "Hello World!";
document.write(str.constructor + "
"); // 输出:function String() { [native code] }
document.write(str.length + "
"); // 输出:10
document.write(str.name); // 输出:Hello World!
var str = new String('JavaScript教程');
document.write(str.anchor("myanchor") + "
"); // 生成一段 HTML 代码:JavaScript教程
document.write(str.big() + "
"); // 生成一段 HTML 代码:JavaScript教程
document.write(str.blink() + "
"); // 生成一段 HTML 代码:
document.write(str.bold() + "
"); // 生成一段 HTML 代码:JavaScript教程
document.write(str.charAt(10) + "
"); // 获取 str 中的第 11 个字符,输出:教
document.write(str.charCodeAt(10) + "
"); // 获取 str 中第 11 个字符的 Unicode 编码,输出:25945
document.write(str.concat(" String 对象") + "
"); // 将字符串“ String 对象”拼接到字符串 str 之后,输出:JavaScript教程 String 对象
document.write(str.fixed() + "
"); // 生成一段 HTML 代码:JavaScript教程
document.write(str.fontcolor("red") + "
"); // 生成一段 HTML 代码:JavaScript教程
document.write(str.fontsize(2) + "
"); // 生成一段 HTML 代码:JavaScript教程
document.write(String.fromCharCode(72,69,76,76,79) + "
"); // 将 Unicode 编码转换为具体的字符,输出:HELLO
document.write(str.indexOf("Script") + "
"); // 获取字符串“Script”在 str 中首次出现的为,输出:4
document.write(str.italics() + "
"); // 生成一段 HTML 代码:JavaScript教程
document.write(str.lastIndexOf("a") + "
"); // 获取字符串“a”在 str 中最后一次出现的位置,输出 3
document.write(str.link("http://www.365tools.cn/") + "
"); // 生成一段 HTML 代码:JavaScript教程
document.write(str.localeCompare("JavaScript") + "
"); // 比较字符串对象与给定字符串,返回:1
document.write(str.match(/[abc]/g) + "
"); // 根据正则 /[abc]/g 检索 str,返回:a,a,c
document.write(str.replace(/[abc]/g, "Y") + "
"); // 使用字符串“Y”替换正则 /[abc]/g 匹配的字符,返回:JYvYSYript教程
document.write(str.search(/[Script]/g) + "
"); // 获取与正则匹配的字符串首次出现的位置,返回:4
document.write(str.slice(6,11) + "
"); // 截取字符串(获取 str 中第 7 到第 11 个字符),返回:ript教
document.write(str.small() + "
"); // 生成一段 HTML 代码:JavaScript教程
document.write(str.split("a") + "
"); // 根据“a”将字符串 str 拆分为数组,返回:J,v,Script教程
document.write(str.strike() + "
"); // 生成一段 HTML 代码:JavaScript教程
document.write(str.sub() + "
"); // 生成一段 HTML 代码:JavaScript教程
document.write(str.substr(3, 7) + "
"); // 从第 4 个字符开始,向后截取 7 个字符,返回:aScript
document.write(str.substring(3, 7) + "
"); // 截取字符串(获取 str 中第 4 到第 7 个字符),返回:aScr
document.write(str.sup() + "
"); // 生成一段 HTML 代码:JavaScript教程
document.write(str.toLocaleLowerCase() + "
"); // 返回:javascript教程
document.write(str.toLocaleUpperCase() + "
"); // 返回:JAVASCRIPT教程
document.write(str.toLowerCase() + "
"); // 返回:javascript教程
document.write(str.toUpperCase() + "
"); // 返回:JAVASCRIPT教程
document.write(str.toString() + "
"); // 返回:JavaScript教程
document.write(str.valueOf() + "
"); // 返回:JavaScript教程
\ 来转义字符串中的引号,如下例所示:var str1 = "He said \"Goodbye\""; var str2 = 'it\'s okay';除了单引号和双引号外,JavaScript 中还提供了一些能够使用反斜线转义的特殊字符,如下表所示:
| 代码 | 输出 | 代码 | 输出 |
|---|---|---|---|
| \' | 单引号 | \r | 回车 |
| \" | 双引号 | \t | tab |
| \\ | 反斜线本身 | \b | 空格 |
| \n | 换行 | \f | 换页 |