码迷,mamicode.com
首页 > Web开发 > 详细

js String对象

时间:2015-06-24 22:27:33      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

字符串(String)

字符串(String)使用长度属性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():

 

字符串转为数组

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

实例

txt="a,b,c,d,e"   // String
txt.split(",");   // Split on commas
txt.split(" ");   // Split on spaces
txt.split("|");   // Split on pipe 
 
 
代码输出
\‘ 单引号
\" 双引号
\\ 斜杆
\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()

js String对象

标签:

原文地址:http://www.cnblogs.com/hank-chen/p/4598649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!