JavaScript 高级(2)

1.JavaScript 字符串(String) 对象

Sting 对象用于处理已有在字符块

JavaScript 字符串

一个字符串用于存储一系列字符就像 "John Doe".

一个字符串可以使用单引号或双引号:

var carname="Volvo XC60";
var carname='Volvo XC60';

你使用位置(索引)可以访问字符串中任何的字符:

var character=carname[7];

字符串的索引从零开始, 所以字符串第一字符为 [0],第二个字符为 [1], 等等。

你可以在字符串中使用引号,如下实例:

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\"";

字符串(String)

字符串(String)使用长度属性lenght来计算字符串的长度:

实例:

var txt="Hello World!"
document.write(txt.length);
var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.write(txt.lenght);

String与Object之间的转换:

实例

       
            
            var str3=new String("hello,world");
            //alert(typeof str3);// 对象类型  object
            //string--转换为-->object--- 使用 new String(字符串值)
            //object--转换为-->string--- 使用toString() /valueoOf()--返回某个字符串对象的string
            var str4=str3.toString();
            alert(typeof str4);

在字符串中查找字符串

字符串使用 indexOf() 来定位字符串中某一个指定的字符首次出现的位置:

实例:

var str="Hello world, welcome to the universe.";
var n=str.indexOf("welcome");

如果没找到对应的字符函数返回-1

lastIndexOf() 方法在字符串末尾开始查找字符串出现的位置。

内容匹配

match()函数用来查找字符串中特定的字符,并且如果找到的话,则返回这个字符。

实例:

var str="Hello world!";
document.write(str.match("world") + "<br>");
document.write(str.match("World") + "<br>");
document.write(str.match("world!"));

替换内容

replace() 方法在字符串中用某些字符替换另一些字符。

实例:

str="Please visit Microsoft!"
var n=str.replace("Microsoft","Runoob");

字符串大小写转换

字符串大小写转换使用函数 toUpperCase() / toLowerCase():

实例:

var txt="Hello World!";       // String
var txt1=txt.toUpperCase();   // txt1 文本会转换为大写
var txt2=txt.toLowerCase();   // txt2 文本会转换为小写

字符串转为数组

字符串使用split()函数转为数组:

实例:

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

特殊字符

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

换页

String 对象属性 

属性

描述

constructor

对创建该对象的函数的引用

length

字符串的长度

prototype

允许您向对象添加属性和方法

String 对象方法 

方法

描述

charAt()

返回在指定位置的字符。

concat()

连接两个或更多字符串,并返回新的字符串。+

indexOf()

返回某个指定的字符串值在字符串中首次出现的位置。

lastIndexOf()

从后向前搜索字符串,并从起始位置(0)开始计算返回字符串最后出现的位置。

match()

查找找到一个或多个正则表达式的匹配。

replace()

在字符串中查找匹配的子串, 并替换与正则表达式匹配的子串。

split()

把字符串分割为字符串数组。

startsWith()

查看字符串是否以指定的子字符串开头。

substr()

从起始索引号提取字符串中指定数目的字符。

substring()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值