1、基本类型String

  1. var str ="helloworld";
要记住:保存的是Unicode字符,一旦创建便不可变
 

2、引用类型String

  1. var strObj =newString("hello world");
 
要点:

字符方法:

  1.     alert (strObj.charAt(0));// 'h'
  2.     alert (strObj.charCodeAt(1));// 101 即 'e' 的 unicode编码
  3.     alert(strObj[1]);  // 'e', ES5
 
 

字符串方法:

  1.     var str2 = strObj.concat(" ","china");// "hello world china";
  2.     alert(strObj);// "hello world";
 
    slice() & substr() & substring()
    一个参数(起始位置):
  1.     alert(strObj.slice(3));// "lo world";
  2.     alert(strObj.substring(3));// "lo world";
  3.     alert(strObj.substr());// "lo world";
 
    
    两个参数(起始位置,终点位置 || 长度):
  1.     alert(strObj.slice(3,7));// "lo w";  从下标3开始,到下标7之前 
  2.     alert(strObj.substring(3,7));// "hel" ;从下标3, 到下标7之前    
  3.     alert(strObj.substr(3,7));  // "lo worl" 从下标3开始,长度为7
 
 
    第二个参数<0(起始位置,终点位置 || 长度 ,策略不同):
  1.     alert(strObj.slice(3,-3));// "lo wo"; 第二个参数 -3 被转换成 -3 + str.length = 8; 来对待。
  2.     alert(strObj.substring(3,-3));// "hel"; 第二个参数 -3 被转换成 0,因为 第二个参数小于第一个参数,然后它们要互换位置。
  3.     alert(strObj.substr(3,-3));// ""(空字符串),它会将第一个参数 3 + str.length ,然后将 第二个参数-3 转换成 0.
 
    

字符串位置方法(每次调用只匹配一次,函数返回匹配的位置):

  1.     alert(strObj.indexOf("o"));//4 从前往后
  2.     alert(strObj.lastIndexOf("o"));//7 从后往前
  3.     
  4.     alert(strObj.indexOf("o",6));// 7 忽略位置6以前的(即使匹配)
  5.     alert(strObj.lastIndexOf("o",6));// 4 忽略位置6以后的(即使匹配)
 
    

trim()方法(删除前置和后置的空格,中间空格不删除):

  1.     var strValue ="              hello world             ";
  2.     alert(strValue.trim());// “hello world”
 
    

字符串大小写转换:

  1.     alert(strObj.toLowerCase());//"hello world"
  2.     alert(strObj.toUpperCase());// "HELLO WORLD";
  3.     alert(strObj.toLocaleLowerCase());// "hello world“
  4.     alert(strObj.toLocaleUpperCase());// ”HELLO WORLD“
 
 

模式匹配:

    match:
  1.     var text ="cat, bat, sat, rat";
  2.     var matches = text.match(/.at/);
  3.     alert(matches.index);// 0
  4.     alert(matches[0]);// cat
  5.     alert(matches.lastIndex);// 0
 
 
    search():
  1.     var pos = text.search(/at/);
  2.     alert(pos);// 1 返回第一个匹配的位置
 
   
    replace();     
    
  1. var result1 = text.replace("at","ond");// "cond, bat, sat, rat";
 
    注意:只替换第一个匹配的位置,所以用此方法无法消除字符串中的空格。
  
  1.   var result2 = text.replace(/at/g,"ond");// ”cond, bond, sond, rond“;
 
    消除字符串的所有空格:
  
  1.   text.replace(/\s/g,"");//用 ”“(空字符串 )替换 所有的 空格,制表符,换行。    
 
    
  1. var a ="hellod a sad asdsa dsa das dsa dsa dsa ";
  2. console.log(a.replace(/\s/g,""));//hellodasadasdsadsadasdsadsadsa VM205:3
  3. var a ="hellod a sad asdsa dsa das dsa dsa dsa ";
  4. console.log(a.replace(" ",""));//helloda sad asdsa dsa das dsa dsa dsa
 
    
    replace()方法的第二个参数也可以是一个函数,这个函数有三个参数(模式的匹配项,模式匹配项在字符串中的位置,原始字符串)
    
  1. function htmlEscape(text){
  2.         return text.replace(/[<>"&]/g,function(match, pos, originalText){
  3.             switch(match){
  4.                 case"<":
  5.                         return"<";
  6.                 case">":
  7.                         return">";
  8.                 case"&":
  9.                         return"&";
  10.                 case"\"":
  11.                         return""";
  12.             }
  13.         });
  14.     }    
 
    

split() ,将字符串分隔,返回分隔后组成的数组

  1.     var colorText ="red,blue,green,yellow";
  2.     var c1 = colorText.split(",");//["red","blue","green","yellow"];
  3.     var c2 = colorText.split(”,“,2);//["red","blue"]; 第二个参数返回的数组的大小。
 
 

localeCompare(),比较字符串

  1.     var strVal ="yellow";
  2.     alert(strVal.localeCompare("black"));// 1
  3.     alert(strVal.localeCompare("yellow"));// 0
  4.     alert(strVal.localeCompare("zoo"));// -1 或其他负数
 
 
 
 

JavaScript中的String的更多相关文章

  1. ExtJS学习-----------Ext.String,ExtJS对javascript中的String的扩展

    关于ExtJS对javascript中的String的扩展,能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 以 ...

  2. 在Javascript中使用String.startsWith和endsWith

    在Javascript中使用String.startsWith和endsWith 在操作字符串(String)类型的时候,startsWith(anotherString)和endsWith(anot ...

  3. 浅谈JavaScript中的string拥有方法的原因

    我们都知道,JavaScript数据类型分两大类,基本类型(或者称原始类型)和引用类型. 基本类型的值是保存在栈内存中的简单数据段,它们是按值访问的.JS中有五种基本类型:Undefined.Null ...

  4. Javascript中的string类型使用UTF-16编码

    2019独角兽企业重金招聘Python工程师标准>>> 在JavaScript中,所有的string类型(或者被称为DOMString)都是使用UTF-16编码的. MDN DOMS ...

  5. JavaScript中为什么string可以拥有方法?

    所有文章搬运自我的个人主页:sheilasun.me 引子 我们都知道,JavaScript数据类型分两大类,基本类型(或者称原始类型)和引用类型. 基本类型的值是保存在栈内存中的简单数据段,它们是按 ...

  6. JavaScript中的String对象

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

  7. JavaScript中的string对象及方法

    string对象 string对象的两种创建 var a="hello"; var b=new String("hello"); //下面是方法 //charA ...

  8. JavaScript 中的string 方法

    创建string的方法 var str ="abc"; var str = new String("abc"); var str = String(" ...

  9. JavaScript中的String对象详解

    1.属性 String对象最常用的属性是length,用于返回字符串对象的长度. 2.方法 CharAt(index)   返回字符串对象中指定索引号组成的字符串,位置的有效值为0到字符串的长度减1. ...

随机推荐

  1. 推荐《HeadFirst设计模式》

    相对于国内初版的<大话设计模式>,HeadFirst真的是更好的选择,虽然看起来很厚.很吓人,但对于初学者而言浅显易懂.直击要点,即使对设计模式熟悉的同学去读这本书相信也有很大的收获.用了 ...

  2. 【转载】区间信息的维护与查询(一)——二叉索引树(Fenwick树、树状数组)

    在网上找到一篇非常不错的树状数组的博客,拿来转载,原文地址. 树状数组 最新看了一下区间的查询与修改的知识,最主要看到的是树状数组(BIT),以前感觉好高大上的东西,其实也不过就这么简单而已. 我们有 ...

  3. Ubuntu 14.04 升级gcc 4.8到gcc 5.x

    简介 有些软件比较新,需要更高的gcc版本,所以需要升级gcc.编译安装比较耗时,所以直接选择bin包就好. 步骤 添加源 sudo add-apt-repository ppa:ubuntu-too ...

  4. 【转】OpenStack奥斯汀峰会Keynotes国内抢先看

    http://www.openstack.cn/?p=5341 OpenStack奥斯汀峰会Keynotes国内抢先看入口:http://www.tudou.com/home/_903780397/i ...

  5. 产生0-9 A-Z a-z

    >题目要求: >>产生26个大写字母 >>产生26个小写字母 >>产生0-9这10个阿拉伯数字 >程序实现: package cn.fury.test; ...

  6. HTTP协议上传boundary确定&下载content-disposition理解

    HTTP协议上传文件-协议 上传文件需要将form标签 的 ENCTYPE 属性设置为 multipart/form-data属性, 与 application/x-www-form-urlencod ...

  7. MVC4中重复使用JQuery Mobile Dialog的做法实践.

    第一步:建立mobile项目类型 第二步:添加针对对话框的的DialogController.cs: 建立这个Controller的目的是此Dlg可以反复使用,把它做成一个固定界面,其他的Contro ...

  8. 【转修正】sql server行版本控制的隔离级别

    在SQL Server标准的已提交读(READ COMMITTED)隔离级别下,一个读操作会和一个写操作相互阻塞.未提交读(READ UNCOMMITTED)虽然不会有这种阻塞,但是读操作可能会读到脏 ...

  9. The type XXX cannot be resolved. It is indirectly referenced from required .class files错误.....

    遇到The type XXX cannot be resolved. It is indirectly referenced from required .class files错误.....,查找的 ...

  10. grunt让Nodejs规范起来

    Aug 17, 2013 Tags: gruntJavascriptnodejs Comments: 9 Comments grunt让Nodejs规范起来 从零开始nodejs系列文章,将介绍如何利 ...