声明

该文部分代码和内容节选自菜鸟教程,仅用作个人学习,特此声明

链接https://www.runoob.com/

JavaScript String

String 对象用于处理已有的字符块

1、字符串 String

  • 一个字符串用于存储一系列字符就像 “John Doe”。字符串可以使用单引号或双引号

    var carname="Volvo XC60";
    var carname='Volvo XC60';
    
  • 使用位置(索引,从0开始)可以访问字符串中任何的字符

    var character=carname[7];
    
  • 可以在字符串中使用引号,或者可以在字符串中使用转义字符( \ )使用引号

    //内外引号不同可直接使用
    var answer="It's alright";
    var answer="He is called 'Johnny'";
    var answer='He is called "Johnny"';
    
    //内外引号相同需要使用转义字符
    var answer='It\'s alright';
    var answer="He is called \"Johnny\"";
    

这些在之前都讲过了,不再一一赘述

  • 字符串长度属性 length :使用长度属性 length 来计算字符串的长度

    var txt="Hello World!";
    document.write(txt.length);//12
    
    var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    document.write(txt.length);//26
    
  • 定位字符串 indexOf() :使用 indexOf() 来定位字符串中某一个指定的字符首次出现的位置

    如果没找到对应的字符函数返回-1; lastIndexOf() 方法在字符串末尾开始查找字符串出现的位置(不是说末尾位为0,向前分别为第1,2,3位……而是从末尾开始向前找,位置还是从前往后0,1,2…….)

    var str = "Click the button to locate where 'locate' first occurs."
    var n=str.indexOf("locate");		//21
    var n=str.lastIndexOf("locate");	//35
    
  • 匹配字符串 match() :用来查找字符串中特定的字符,并且如果找到的话,则返回这个字符

    document.write(str.match("world") + "<br>");	//world
    document.write(str.match("World") + "<br>");	//null
    document.write(str.match("world!"));			//world!
    
  • 替换内容 replace() :在字符串中用某些字符替换另一些字符

    str="Please visit Microsoft!"
    var n=str.replace("Microsoft","Baidu");	//str = "Please visit Baidu!"
    
  • 字符串大小写转换 toUpperCase() / toLowerCase()

    var txt="Hello World!";       // String
    var txt1=txt.toUpperCase();   // HELLO WORLD!
    var txt2=txt.toLowerCase();   // hello world!
    
  • 字符串转数组 split()

    txt="a,b,c,d,e"   // String
    txt.split(",");   // 使用逗号分隔 
    txt.split(" ");   // 使用空格分隔 
    txt.split("|");   // 使用竖线分隔 
    

2、特殊字符

Javascript 中可以使用反斜线(\)插入特殊符号,如:撇号,引号等其他特殊符号。

查看如下 JavaScript 代码:

var txt="We are the so-called "Vikings" from the north.";
document.write(txt);

在JavaScript中,字符串的开始和停止使用单引号或双引号。这意味着,上面的字符串将被切成: We are the so-called

解决以上的问题可以使用反斜线来转义引号:

var txt="We are the so-called \"Vikings\" from the north.";
document.write(txt);

JavaScript将输出正确的文本字符串:We are the so-called “Vikings” from the north.

下表列出其他特殊字符,可以使用反斜线转义特殊字符:

代码 输出
\' 单引号
\" 双引号
\\ 斜杆
\n 换行
\r 回车
\t tab
\b 空格
\f 换页

3、字符串属性和方法

参考:JavaScript String 对象

4、补充知识

1.关于 JavaScript 里的 trim()/strip() 方法。在其他语言中常常用 trim()/strip() 方法脱去不必要的空格等元素。

JavaScript 本身并不提供 trim() 方法,不过可以用正则表达式,通过给 String 对象增加 trim 方法的方式实现。

如下:

if(typeof(String.prototype.trim) === "undefined")
{
    String.prototype.trim = function() 
    {
        return String(this).replace(/^\s+|\s+$/g, '');
    };
}

if(" dog  ".trim() === "dog") {
    document.write("成功去除首尾空格");		//成功去除首尾空格
} 

2.replace() 方法若想一次进行全局替换可以在后面加上 g,若想替换的内容忽略大小写 则加上 i

如下例子将字符串中的字符 a(不区分大小写) 替换为 x

var sText = "abcdefaABC";
//g 代表全局替换  i 代表 忽略大小写
var txt = sText.replace( /a/gi , 'x');
document.getElementById("demo").innerHTML = txt;//xbcdefxxBC

3.关于字符串的定位需要注意:

  • indexOf(‘a’, 5) 查找的是字符串前5位之后第一个a(也就是说忽略掉前5位(0~4位置)的a)
  • lastIndexOf(‘a’, 6) 查找的是字符串前6位(0~5位置)之内最后一个a
<p id="p1">aaaaa vaaaa</p>
<p id="p2"></p>
<p id="p3"></p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
	var str=document.getElementById("p1").innerHTML;
	var x = str.indexOf('a',5);
	var y = str.lastIndexOf('a',6);
	document.getElementById("p2").innerHTML = x;	//7
	document.getElementById("p3").innerHTML = y;	//4

}
</script>
  • 千万不要混淆把 lastIndexOf(‘a’, 7) 理解成查找字符串倒数第7位中的a
版权声明:本文为xypersonal原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xypersonal/p/16244186.html