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

var str="this is a demo";

alert(str[3])//    s

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

2 在字符串中查找字符串

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

实例

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

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

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

3 内容匹配

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

实例

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

4 替换内容

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

实例

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

5 字符串大小写转换

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

实例

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

6 字符串转为数组

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

实例

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

7 特殊字符

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 对象的更多相关文章

  1. JavaScript String对象

    本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...

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

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

  3. js实用篇之String对象

    概述 String对象是JavaScript原生提供的三个包装对象之一,用来生成字符串的包装对象. var s1 = 'abc'; var s2 = new String('abc'); typeof ...

  4. java 创建string对象机制 字符串缓冲池 字符串拼接机制

    对于创建String对象的机制,在这一过程中涉及的东西还是值得探究一番的. 首先看通过new String对象和直接赋值的方式有什么区别,看如下代码: public static void main( ...

  5. 原生JS:String对象详解

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  6. JavaScript String 对象

    JavaScript String 对象 String 对象 String 对象用于处理文本(字符串). String 对象创建方法: new String(). 语法 var txt = new S ...

  7. js String对象中常用方法小结(字符串操作)

    1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码. strObj.charCodeAt(index) 说明: index将被处理字符的从零开始计数的编号.有效值为0到字符 ...

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

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

  9. JavaScript中的String对象

        String对象提供的方法用于处理字符串及字符. 常用的一些方法: charAt(index):返回字符串中index处的字符. indexOf(searchValue,[fromIndex] ...

  10. JavaScript基础13——js的string对象

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

随机推荐

  1. hdu 3371

    #include<stdio.h> #include<stdlib.h> #define N 501 struct node { int x,y,dis; }road[N*N] ...

  2. Educational Codeforces Round 50 (Rated for Div. 2) E. Covered Points

    注释上都有解析了,就不写了吧,去重的问题就用set解决,并且呢第i个线段最多和其他线段产生i-1个交点,n^2logn. #include <cmath> #include <cst ...

  3. uva 11691

    贪心 ~~ 使用优先队列 #include <cstdio> #include <cstdlib> #include <cmath> #include <se ...

  4. Java面试题总结(一)---Java基础

    Java面试题总结(一)---Java基础 1.面向对象的特征有哪些? 答:面向对象的特征主要有以下几个: 1)抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方 ...

  5. 扫描仪共享工具(BlindScanner Pro) 3.23 特别版

    http://www.xdowns.com/soft/1/126/2014/Soft_125206.html

  6. 火狐firefox、谷歌chrome等浏览器扩展、插件介绍

    火狐旧的插件扩展已经不可用,需要更新,这是本人安装的最新的插件 chrome插件 https://www.zhihu.com/question/68338297

  7. Gerrit实现代码审计(code review)

    1.Gerrit是个啥? 实现代码审计,比git(gitlab.github)多了代码审计的功能. 2.两类角色 角色1:我是代码编写者,只能写代码和提交给审计者代码,不能直接把代码合并到线上发布 角 ...

  8. 全文搜索引擎 Elasticsearch 安装

    全文搜索引擎 Elasticsearch 安装 学习了:http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html 拼音:https://www ...

  9. wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合

    wpf 导出Excel   1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 4 ExportDataGrid ...

  10. 模仿猫眼电影App一个动画效果

    看真正的猫眼效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzIxMDYyMA==/font/5a6L5L2T/fontsize/400/f ...