ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

string基本包装类型

2021-02-19 12:32:11  阅读:251  来源: 互联网

标签:string 包装 var alert 字符串 world 类型 stringValue hello


目录

以数组的形式访问字符

拼接字符串concat(任意数量参数):

基于原字符串创建新字符串slice()、 substring()和substr():

定位子字符串的位置:

去除字符串前后空格trim():

字符串大小写转换:

字符串模式匹配match():

search():

替换子字符串replace():

将字符串按指定的分隔符分割成数组split():

字符串比较localeCompare():

编码转字符串fromCharCode():

HTML 方法:


String 类型的每个实例都有一个 length 属性,表示字符串中包含多个字符

var stringValue = "hello world";
alert(stringValue.length); //"11"

访问指定索引处的字符字符编码:charAt()、charCodeAt()

var stringValue = "hello world";
alert(stringValue.charAt(1)); //"e"

以数组的形式访问字符

var stringValue = "hello world";
alert(stringValue[1]); //"e"

拼接字符串concat(任意数量参数):

返回新字符串,不会更改原有字符串。+号运算符也可用于拼接字符串,最为常用。

var stringValue = "hello ";
var result = stringValue.concat("world");
alert(result); //"hello world"
alert(stringValue); //"hello" 

基于原字符串创建新字符串slice()、 substring()和substr():

接受两个参数,第一个参数指定起始位置;第二个参数(可选)默认到字符串结束,slice和substring指定结束索引,substr()指定截取字符串数量

var stringValue = "hello world";
alert(stringValue.slice(3)); //"lo world"
alert(stringValue.substring(3)); //"lo world"
alert(stringValue.substr(3)); //"lo world"
alert(stringValue.slice(3, 7)); //"lo w"
alert(stringValue.substring(3,7)); //"lo w"
alert(stringValue.substr(3, 7)); //"lo worl" 

索引为负的情况:其中,slice()方法会将传 入的负值与字符串的长度相加,substr()方法将负的第一个参数加上字符串的长度,而将负的第二个 参数转换为 0。最后,substring()方法会把所有负值参数都转换为 0。

substring()方法会把第二个参数转换为 0,使调 用变成了 substring(3,0),而由于这个方法会将较小的数作为开始位置,将较大的数作为结束位置, 因此最终相当于调用了 substring(0,3)

var stringValue = "hello world";
alert(stringValue.slice(-3)); //"rld"
alert(stringValue.substring(-3)); //"hello world"
alert(stringValue.substr(-3)); //"rld"
alert(stringValue.slice(3, -4)); //"lo w"
alert(stringValue.substring(3, -4)); //"hel"
alert(stringValue.substr(3, -4)); //""(空字符串)

定位子字符串的位置:

indexOf()和 lastIndexOf(),indexOf()方法从字符串的开头向后搜索子字符串,而 lastIndexOf()方法 是从字符串的末尾向前搜索子字符串

var stringValue = "hello world";
alert(stringValue.indexOf("o")); //4
alert(stringValue.lastIndexOf("o")); //7

这两个方法都可以接收可选的第二个参数,表示从字符串中的哪个位置开始搜索。换句话说, indexOf()会从该参数指定的位置向后搜索,忽略该位置之前的所有字符;而 lastIndexOf()则会从 指定的位置向前搜索,忽略该位置之后的所有字符

var stringValue = "hello world";
alert(stringValue.indexOf("o", 6)); //7
alert(stringValue.lastIndexOf("o", 6)); //4

搜索所有匹配的子字符串

var stringValue = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
var positions = new Array();
var pos = stringValue.indexOf("e");
while(pos > -1){
 positions.push(pos);
 pos = stringValue.indexOf("e", pos + 1);
}

alert(positions); //"3,24,32,35,52" 

去除字符串前后空格trim():

返回副本,不更改原字符串

var stringValue = " hello world ";
var trimmedStringValue = stringValue.trim();
alert(stringValue); //" hello world "
alert(trimmedStringValue); //"hello world" 

字符串大小写转换:

toLowerCase()、toLocaleLowerCase()、toUpperCase()和 toLocaleUpperCase()。 其中,toLowerCase()和 toUpperCase()是两个经典的方法,借鉴自 java.lang.String 中的同名 方法。而 toLocaleLowerCase()和 toLocaleUpperCase()方法则是针对特定地区的实现。对有些地 区来说,针对地区的方法与其通用方法得到的结果相同,但少数语言(如土耳其语)会为 Unicode 大小 写转换应用特殊的规则,这时候就必须使用针对地区的方法来保证实现正确的转换

var stringValue = "hello world";
alert(stringValue.toLocaleUpperCase()); //"HELLO WORLD"
alert(stringValue.toUpperCase()); //"HELLO WORLD"
alert(stringValue.toLocaleLowerCase()); //"hello world"
alert(stringValue.toLowerCase()); //"hello world" 

字符串模式匹配match():

match()方法只接受一个参数,要么是一 个正则表达式,要么是一个 RegExp 对象。match()方法返回了一个数组;如果是调用 RegExp 对象的 exec()方法并传递本例中的 字符串作为参数,那么也会得到与此相同的数组:数组的第一项是与整个模式匹配的字符串,之后的每 一项(如果有)保存着与正则表达式中的捕获组匹配的字符串。

var text = "cat, bat, sat, fat";
var pattern = /.at/;
//与 pattern.exec(text)相同
var matches = text.match(pattern);
alert(matches.index); //0
alert(matches[0]); //"cat"
alert(pattern.lastIndex); //0 

search():

这个方法的唯一参数与 match()方法的参数相同:由字 符串或 RegExp 对象指定的一个正则表达式。search()方法返回字符串中第一个匹配项的索引;如果没 有找到匹配项,则返回-1。而且,search()方法始终是从字符串开头向后查找模式

var text = "cat, bat, sat, fat";
var pos = text.search(/at/);
alert(pos); //1

替换子字符串replace():

这个方法接受两个参数:第 一个参数可以是一个 RegExp 对象或者一个字符串(这个字符串不会被转换成正则表达式),第二个参 数可以是一个字符串或者一个函数。如果第一个参数是字符串,那么只会替换第一个子字符串。要想替 换所有子字符串,唯一的办法就是提供一个正则表达式,而且要指定全局(g)标志

var text = "cat, bat, sat, fat";
var result = text.replace("at", "ond");
alert(result); //"cond, bat, sat, fat"
result = text.replace(/at/g, "ond");
alert(result); //"cond, bond, sond, fond" 

如果第二个参数是字符串,那么还可以使用一些特殊的字符序列,将正则表达式操作得到的值插入 到结果字符串中

var text = "cat, bat, sat, fat";
result = text.replace(/(.at)/g, "word ($1)");
alert(result); //word (cat), word (bat), word (sat), word (fat) 

 如果第二个参数是函数可以实现更精确的字符串替换:在只有一个匹配项(即与模式匹配的字符串)的 情况下,会向这个函数传递 3 个参数:模式的匹配项、模式匹配项在字符串中的位置和原始字符串。在 正则表达式中定义了多个捕获组的情况下,传递给函数的参数依次是模式的匹配项、第一个捕获组的匹 配项、第二个捕获组的匹配项……,但最后两个参数仍然分别是模式的匹配项在字符串中的位置和原始 字符串。这个函数应该返回一个字符串,表示应该被替换的匹配项使用函数作为 replace()方法的第 二个参数

function htmlEscape(text){
 return text.replace(/[<>"&]/g, function(match, pos, originalText){
 switch(match){
 case "<":
 return "&lt;";
 case ">":
 return "&gt;";
 case "&":
 return "&amp;";
 case "\"":
 return "&quot;";
 }
 });
}
alert(htmlEscape("<p class=\"greeting\">Hello world!</p>"));
//&lt;p class=&quot;greeting&quot;&gt;Hello world!&lt;/p&gt;

将字符串按指定的分隔符分割成数组split():

分隔符可以是字符串,也可以是一个 RegExp 对象(这个方 法不会将字符串看成正则表达式)。split()方法可以接受可选的第二个参数,用于指定数组的大小, 以便确保返回的数组不会超过既定大小.

对 split()中正则表达式的支持因浏览器而异。尽管对于简单的模式没有什么差别,但对于未发现 匹配项以及带有捕获组的模式,匹配的行为就不大相同了。以下是几种常见的差别。  IE8 及之前版本会忽略捕获组。ECMA-262 规定应该把捕获组拼接到结果数组中。IE9 能正确地 在结果中包含捕获组。  Firefox 3.6 及之前版本在捕获组未找到匹配项时,会在结果数组中包含空字符串;ECMA-262 规 定没有匹配项的捕获组在结果数组中应该用 undefined 表示。

var colorText = "red,blue,green,yellow";
var colors1 = colorText.split(","); //["red", "blue", "green", "yellow"]
var colors2 = colorText.split(",", 2); //["red", "blue"]
var colors3 = colorText.split(/[^\,]+/); //["", ",", ",", ",", ""]

字符串比较localeCompare():

根据字符串在字母表中的位置返回-1,0,1。localeCompare()方法比较与众不同的地方,就是实现所支持的地区(国家和语言)决定了这个 方法的行为。比如,美国以英语作为 ECMAScript 实现的标准语言,因此 localeCompare()就是区分 大小写的,于是大写字母在字母表中排在小写字母前头就成为了一项决定性的比较规则。不过,在其他 地区恐怕就不是这种情况了

var stringValue = "yellow";
alert(stringValue.localeCompare("brick")); //1
alert(stringValue.localeCompare("yellow")); //0
alert(stringValue.localeCompare("zoo")); //-1

编码转字符串fromCharCode():

alert(String.fromCharCode(104, 101, 108, 108, 111)); //"hello"

HTML 方法:

用 JavaScript 动态格式化 HTML,尽量不使用这些方法,因为它们创建的标记通常无法表达语义

标签:string,包装,var,alert,字符串,world,类型,stringValue,hello
来源: https://blog.csdn.net/z1844306480/article/details/113860476

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有