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);

尝试一下 »

JavaScript 获取字符串的长度:通过在字符串变量或字符串后面写上 .length 来获得变量中string (字符串)值的长度。


在字符串中查找字符串

字符串使用 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

尝试一下 »


字符串转为数组

字符串使用string>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()

JavaScript 字符串(String)对象的更多相关文章

  1. JavaScript 字符串(String)对象

    String 对象 String 对象用于处理文本(字符串). 创建 String 对象的语法: new String(s); String(s); 参数 参数 s 是要存储在 String 对象中或 ...

  2. JavaScript 字符串(String)对象的方法

    anchor() 描述:用于创建 HTML 锚 原型:stringObject.anchor(anchorname) 用法: <script> var txt="Hello wo ...

  3. JavaScript中String对象的match()、replace() 配合正则表达式使用

    正则表达式由来已久,查找替换功能非常强大,但模板难记复杂. JavaScript中String对象的match().replace()这2个方法都要使用正则表达式的模板.当模板内容与字符串不相匹配时, ...

  4. JavaScript中String对象的方法介绍

    1.字符方法 1.1 charAt() 方法,返回字符串中指定位置的字符. var question = "Do you like JavaScript?"; alert(ques ...

  5. Javascript中String对象的的简单学习

    第十一课String对象介绍1:属性    在javascript中可以用单引号,或者双引号括起来的一个字符当作    一个字符对象的实例,所以可以在某个字符串后再加上.去调用String    对象 ...

  6. Javascript数组,String对象,Math对象,Date对象,正则表达式

    标题栏的滚动<html><head><title>山西众创金融</title></head>function init(){ //1.拿到标 ...

  7. JavaScript字符串String

    JavaScript中String类型用于表示由零个或者多个16位Unicode字符组成的字符序列即字符串:同时字符串可以用单引号或双引号表示. 下面是一些特殊的字面量: 字面量 含义\n 换行\t ...

  8. JavaScript:String 对象

    ylbtech-JavaScript:String 对象 1.返回顶部 String 对象 String 对象用于处理文本(字符串). 创建 String 对象的语法: new String(s); ...

  9. js 判断字符串是否包含某字符串,String对象中查找子字符,indexOf

    var Cts = "bblText";   if(Cts.indexOf("Text") > 0 ) {     alert('Cts中包含Text字符 ...

  10. JavaScript的String对象

    1.创建String对象 Html标签的格式编排方法:可以将String对象的字符串内容输出成对应的html标签. 示例: var str = "JavaScript程序设计"; ...

随机推荐

  1. Qt QFile文件读写

    QFile 需要添加 #Include  <QFile> 集成至QIODevice 打开一个文件有3种方式QIODevice::(ReadOnly/WriteOnly/ReadWrite) ...

  2. semver(Semantic Versioning)

    Based on semver, you can use Hyphen Ranges X.Y.Z - A.B.C 1.2.3-2.3.4 Indicates >=1.2.3 <=2.3.4 ...

  3. DVA框架统一处理所有页面的loading状态

    dva 有一个管理 effects 执行的 hook,并基于此封装了 dva-loading 插件.通过这个插件,我们可以不必一遍遍地写 showLoading 和 hideLoading,当发起请求 ...

  4. MySQL中的查询子句

    查询语句 字句名称 使用目的 select 确定结果集中应该包含那些列 from 指明所要提取数据的表,以及这些表是如何连接的 where 过滤不需要的数据 group by 用于对具有想用列值的行进 ...

  5. php Redis函数使用总结(string,hash,list, set , sort set )

    对于:string, set , sort set , hash 的增,改操作,是同一个命令,但是把它当改操作时,及时成功返回值依旧为0 对于:list结构来说,增删改查自有一套方法.   <? ...

  6. [C#]设计模式-抽象工厂-创建型模式

    介绍了简单工厂与工厂方法之后,现在我们来看一下工厂三兄弟的最后一个 -- 抽象工厂. 那什么是抽象工厂呢? 抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相 ...

  7. [LeetCode] Minimum Factorization 最小因数分解

    Given a positive integer a, find the smallest positive integer b whose multiplication of each digit ...

  8. [LeetCode] Tag Validator 标签验证器

    Given a string representing a code snippet, you need to implement a tag validator to parse the code ...

  9. 机器学习技法:04 Soft-Margin Support Vector Machine

    Roadmap Motivation and Primal Problem Dual Problem Messages behind Soft-Margin SVM Model Selection S ...

  10. [ZJOI2010]基站选址

    题目描述 有N个村庄坐落在一条直线上,第i(i>1)个村庄距离第1个村庄的距离为Di.需要在这些村庄中建立不超过K个通讯基站,在第i个村庄建立基站的费用为Ci.如果在距离第i个村庄不超过Si的范 ...