博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript 字符串(String) 对象
阅读量:4547 次
发布时间:2019-06-08

本文共 2260 字,大约阅读时间需要 7 分钟。

JavaScript 字符串(String) 对象


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


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)使用长度属性length来计算字符串的长度:

实例

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

在字符串中查找字符串

字符串使用 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","w3cschool");

 


字符串大小写转换

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

实例

var txt="Hello World!";       // String
var txt1=txt.toUpperCase();   // txt1 is txt converted to upper
var txt2=txt.toLowerCase();   // txt2 is txt converted to lower

 


字符串转为数组

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

实例

txt="a,b,c,d,e"   // String
txt.split(",");   // Split on commas
txt.split(" ");   // Split on spaces
txt.split("|");   // Split on pipe 

 


特殊字符

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 换页

 


字符串属性和方法

属性:

  • length
  • prototype
  • constructor

方法:

    • charAt()
    • charCodeAt()
    • concat()
    • fromCharCode()
    • indexOf()
    • lastIndexOf()
    • match()
    • replace()
    • search()
    • slice()
    • split()
    • substr()
    • substring()
    • toLowerCase()
    • toUpperCase()
    • valueOf()

转载于:https://www.cnblogs.com/aiqingqing/p/4545110.html

你可能感兴趣的文章
扁平化前端皮肤效果
查看>>
分布式文件系统hdfs——dfs命令
查看>>
AVR单片机入门
查看>>
团队作业第5周 - 测试与发布(Alpha版本)- 天冷记得穿秋裤队
查看>>
javascript-焦点图实现(四)
查看>>
影院售票系统
查看>>
银联在线支付B2C UnionPay.NET
查看>>
开关电源中的电阻和电容串联起来有那些作用
查看>>
ld链接脚本文件语法解析之一
查看>>
linux的socket CAN驱动介绍(code)_good
查看>>
Hadoop集群搭建
查看>>
分享我见到的培训面试和就业的情况(同时给出建议)
查看>>
简单-爬取豆瓣top250-成员简介及分工
查看>>
一次dropzone体验
查看>>
(小组)目前流行的单元测试工具有哪些
查看>>
近期需要整理android相关知识点
查看>>
算法中的思想(第0篇)
查看>>
puppet使用rsync模块同步目录和文件
查看>>
有监督学习以及偏差和过拟合的概念
查看>>
MATLAB axis用法
查看>>