JS中的String类型

String类型是字符串的对象包装类型,可以像下面这样使用String构造函数来创建。

 var stringObject = new String("hello world");

下面讲述了字符串可以使用的一些方法。

一,字符方法

1、用于访问字符串中特定字符的两个方法:charAt()和charCodeAt()
这两个方法都接收一个参数,即基于0的字符位置。
charAt()方法以单字符串的形式返回给定位置的那个字符。
charAtCode()方法返回给定位置的那个字符的字符编码。
示例如下:

 var stringValue = "hello world";
  console.log(stringValue.charAt(1));//e
  console.log(stringValue.charCodeAt(1));//101

ECMAScript5还定义了另一个访问个别字符的方法,可以使用方括号加数字索引来访问字符串中的特殊字符,如下所示

var stringValue = "hello world";
 console.log(stringValue[1]);//e

2、frommCharCode()方法:
string构造函数的一个静态方法,该方法的任务是接受一或多个字符编码,然后将它们转化成字符串,该方法与charCodeAt()执行的是相反的操作,如下:

alert(String.fromCharCode(104,101,108,108,111));//"hello"
二,字符串操作方法

1,concat()方法:
用于将一个或多个字符串拼接起来,返回拼接得到的新字符串。该方法可以接收任意多个参数,第一个参数是第一个要拼接的字符串,第二个参数是再接着要拼接的字符串。
2,ECMAScript还提供了三个基于子字符串创建新字符串的方法:slice(),substr()和substring()。

  • 该三个方法都会返回被操作字符串的一个子字符串,也都接收一或两个参数。
  • 第一个参数指定子字符串的开始位置,第二个参数(在指定的情况下)表示子字符串到哪里结束
  • 具体来说slice()和substring()的第二个参数指定的是子字符串最后一个字符后面的位置。而substr()的第二个参数指定的则是返回的字符个数。
  • 如果没给这些方法传递第二个参数,则将字符串的末尾作为结束位置。
  • 这些方法对原字符串没有任何影响
    示例如下:
  var stringValue = "hello world";
  console.log(stringValue.slice(3));//"lo world"
  console.log(stringValue.substring(3));//"lo world"
  console.log(stringValue.substr(3));//"lo world"
  console.log(stringValue.slice(3,7));//"lo w"
  console.log(stringValue.substring(3,7));//"lo w"
  console.log(stringValue.substr(3,7));//"lo worl"

在传递给这些方法的参数是负值的情况下,结果就不尽相同。

  • slice()方法会将传入的的负值与字符串的长度相加
  • substr()方法会将负的第一个参数加上字符串的长度,而将负的第二个参数转化为0
  • substring()方法会把所有负值参数都转化为0
  var stringValue = "hello world";
  console.log(stringValue.length);//11;在此处先输出一下字符串的长度
  console.log(stringValue.slice(-3));//"rld";相当于stringValue.slice(8)
  console.log(stringValue.substring(-3));//"hello world";相当于stringValue.substring(0)
  console.log(stringValue.substr(-3));//"rld";相当于stringValue.substr(8)
  console.log(stringValue.slice(3,-4));//"lo w";相当于stringValue.slice(3,7)
  console.log(stringValue.substring(3,-4));//"hel";相当于stringValue.substring(3,7)
  console.log(stringValue.substr(3,-4));//"";相当于stringValue.substr(3,0)
三,字符串位置方法

1,indexof()和lastIndexOf()方法:和数组里面的方法的用法一样,在博文数组里面的方法有详细介绍
2,trim()方法:该方法会创建一个字符串的副本,删除前置及后缀的所有空格,然后返回结果

  var stringValue = "  hello world  ";
  var trimmedStringValue = stringValue.trim();
  console.log(stringValue);//"  hello world  "
  console.log(trimmedStringValue);//"hello world"
四,字符串大小写转换方法

ECMAScript中涉及字符串大小写转换的方法有4个:toLowerCase(), toLocaleLowerCase(), toUpperCase(), toLocaleUpperCase()

  • 其中,toLowerCase()和toUpperCase()是两个经典的方法,借鉴于java.lang.String中的同名方法。
  • toLocaleLowerCase()和toLocaeUpperCase()方法则是针对特定地区的实现,对有些地区来说,针对地区的方法与其通用方法得到的结果相同,但少数语言(如土耳其语)会为Unicode大小写转换应用特殊的规则,这时候必须使用针对地区的方法来保证实现正确的转换。
    示例如下:
  var stringValue = "hello world";
  console.log(stringValue.toUpperCase());//HELLO WORLD
  console.log(stringValue.toLocaleUpperCase());//HELLO WORLD
  var newValue = stringValue.toUpperCase();
  console.log(newValue);//HELLO WORLD
  console.log(stringValue.toLowerCase());//hello world
  console.log(stringValue.toLocaleLowerCase());//hello world
五,字符串的模式匹配方法

1,match()方法
在字符串上调用这个方法,本质上与调用RegExp的exec()方法相同。该方法直接收一个参数,要么是一个正则表达式,要么是一个RegExp对象,然后返回一个数组。看下面的例子:

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

解析:本例中的match()方法返回一个数组;如果是调用RegExp对象的exec()方法并传递本例中的字符串,那么也会得到与此相同的数组:数组的第一项是与整个模式匹配的字符串,之后的每一项( 如果有)保存着与正则表达式中的捕获组匹配的字符串。
2,search()方法
该方法唯一的一个参数是由字符串或RegExp对象指定的一个正则表达式。
该方法返回字符串中第一个匹配项的索引,如果没有一个匹配项则返回-1

  var text = "cat bat sat fat";
  var searchResult = text.search(/a/);
  console.log(searchResult);//1;返回了1即 “at"在字符串中第一次出现的位置

3,replace()方法

ECMAScript()提供了这个方法,是为了简化替换字符串的操作
该方法接收两个参数:第一个参数可以是一个RegExp对象或者一个字符串(该字符串不会被转换成正则表达式),第二个参数可以是一个字符串或者一个函数。

  • 如果第一个参数是字符串,那么只会替换第一个子字符串。要替换所有子字符串,唯一的办法就是提供一个正则表达式,而且要指定全局(g)标志
    如下所示:
 var text = "cat bat sat fat";
 var result_A = text.replace("at","ond");
 console.log(result_A);//"cond bat sat fat"
 var result_B = text.replace(/at/g,"ond");
 console.log(result_B);//"cond bond sond fond"

解析:在上面的例子中,传入repalce()方法的是字符串"at"和替换用的”ond"。替换的结果是把"cat"变成了"cond",但它只会把寻找到的第一个at换成要替换的,然后就会停止寻找。所以字符串中其他字符串就不会受到影响。
然后又将第一个参数修改为带有全局标志的正则表达式,就会在整个字符串中寻找"at",就将全部的"at"都替换成了“ond"。

  • 如果第二个参数是字符串,那么还可以使用一些特殊的字符序列,将正则表达式操作得到的值插入到结果字符串中。下表列出了ECMAScript提供这些特殊字符序列:
字符序列替换文本
$$$
$&匹配整个模式的字字符串,与RegExp.lastMatch的值相同
$‘匹配的子字符串之前的子字符串,与RegExp.leftContent的值相同
$`匹配的子字符串之后的子字符串,与RegExp.rightContent的值相同
$n匹配第n个捕获组的字符串,其中n等于0-9;$1匹配第一个捕获组的字符串,以此类推
$nn匹配第nn个捕获组的字符串,其中nn等于01-99;$01匹配第一个捕获组的字符串,以此类推
  • repalce()方法的第二个参数是函数时,在只有一个匹配项(即与模式匹配的字符串)的情况下,会向这个函数传递3个参数:模式的匹配项、模式匹配项在字符串中的位置、原始字符串。在正则表达式中定义了多个捕获组的情况下,传递给函数的参数依次是模式的匹配项、第一个捕获组的匹配项、第二个捕获组的匹配项······,但最后两个参数仍然分别是模式的匹配项在字符串中的位置和原始字符串。
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>"));//&gt;p class='greeting'&lt;hello world!&gt;/p&lt;
六、localeCompare()方法

该方法比较两个字符串,并返回下列值中的一个:

  • 如果字符串在子母表中应该排在字符串参数之前,则返回一个负数(大多数情况下是-1,具体的值要视实现而定)
  • 如果字符串等于字符串参数,则返回0
  • 如果字符串在字母表中应该排在字符串参数之后,则返回一个正数(大多数情况是1,具体值要视实现而定)
    例子如下:
 var stringValue = "yellow";
 function determineOrder(value){
     var result = stringValue.localeCompare(value);
     if(result < 0){
         alert("the string 'yellow' comes before the string '"+value+"'");
     } else if(result > 0){
         alert("the string 'yellow' come after the string '"+value+"'");
     } else{
         alert("the string 'yellow' is equal to the string '"+value +"'");
     }
 }
 determineOrder("brick");//the string 'yellow' come after the string 'brick'
 determineOrder("yellow");//the string 'yellow' is equal to the string 'yellow'
 determineOrder("zoo");//the string 'yellow' comes before the string 'zoo'
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值