JavaScript中的String
1、基本类型String
var str ="helloworld";
2、引用类型String
var strObj =newString("hello world");
字符方法:
alert (strObj.charAt(0));// 'h'alert (strObj.charCodeAt(1));// 101 即 'e' 的 unicode编码alert(strObj[1]); // 'e', ES5
字符串方法:
var str2 = strObj.concat(" ","china");// "hello world china";alert(strObj);// "hello world";
alert(strObj.slice(3));// "lo world";alert(strObj.substring(3));// "lo world";alert(strObj.substr());// "lo world";
alert(strObj.slice(3,7));// "lo w"; 从下标3开始,到下标7之前alert(strObj.substring(3,7));// "hel" ;从下标3, 到下标7之前alert(strObj.substr(3,7)); // "lo worl" 从下标3开始,长度为7
alert(strObj.slice(3,-3));// "lo wo"; 第二个参数 -3 被转换成 -3 + str.length = 8; 来对待。alert(strObj.substring(3,-3));// "hel"; 第二个参数 -3 被转换成 0,因为 第二个参数小于第一个参数,然后它们要互换位置。alert(strObj.substr(3,-3));// ""(空字符串),它会将第一个参数 3 + str.length ,然后将 第二个参数-3 转换成 0.
字符串位置方法(每次调用只匹配一次,函数返回匹配的位置):
alert(strObj.indexOf("o"));//4 从前往后alert(strObj.lastIndexOf("o"));//7 从后往前alert(strObj.indexOf("o",6));// 7 忽略位置6以前的(即使匹配)alert(strObj.lastIndexOf("o",6));// 4 忽略位置6以后的(即使匹配)
trim()方法(删除前置和后置的空格,中间空格不删除):
var strValue =" hello world ";alert(strValue.trim());// “hello world”
字符串大小写转换:
alert(strObj.toLowerCase());//"hello world"alert(strObj.toUpperCase());// "HELLO WORLD";alert(strObj.toLocaleLowerCase());// "hello world“alert(strObj.toLocaleUpperCase());// ”HELLO WORLD“
模式匹配:
var text ="cat, bat, sat, rat";var matches = text.match(/.at/);alert(matches.index);// 0alert(matches[0]);// catalert(matches.lastIndex);// 0
var pos = text.search(/at/);alert(pos);// 1 返回第一个匹配的位置
var result1 = text.replace("at","ond");// "cond, bat, sat, rat";
var result2 = text.replace(/at/g,"ond");// ”cond, bond, sond, rond“;
text.replace(/\s/g,"");//用 ”“(空字符串 )替换 所有的 空格,制表符,换行。
var a ="hellod a sad asdsa dsa das dsa dsa dsa ";console.log(a.replace(/\s/g,""));//hellodasadasdsadsadasdsadsadsa VM205:3var a ="hellod a sad asdsa dsa das dsa dsa dsa ";console.log(a.replace(" ",""));//helloda sad asdsa dsa das dsa dsa dsa
function htmlEscape(text){return text.replace(/[<>"&]/g,function(match, pos, originalText){switch(match){case"<":return"<";case">":return">";case"&":return"&";case"\"":return""";}});}
split() ,将字符串分隔,返回分隔后组成的数组
var colorText ="red,blue,green,yellow";var c1 = colorText.split(",");//["red","blue","green","yellow"];var c2 = colorText.split(”,“,2);//["red","blue"]; 第二个参数返回的数组的大小。
localeCompare(),比较字符串
var strVal ="yellow";alert(strVal.localeCompare("black"));// 1alert(strVal.localeCompare("yellow"));// 0alert(strVal.localeCompare("zoo"));// -1 或其他负数
JavaScript中的String的更多相关文章
- ExtJS学习-----------Ext.String,ExtJS对javascript中的String的扩展
关于ExtJS对javascript中的String的扩展,能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 以 ...
- 在Javascript中使用String.startsWith和endsWith
在Javascript中使用String.startsWith和endsWith 在操作字符串(String)类型的时候,startsWith(anotherString)和endsWith(anot ...
- 浅谈JavaScript中的string拥有方法的原因
我们都知道,JavaScript数据类型分两大类,基本类型(或者称原始类型)和引用类型. 基本类型的值是保存在栈内存中的简单数据段,它们是按值访问的.JS中有五种基本类型:Undefined.Null ...
- Javascript中的string类型使用UTF-16编码
2019独角兽企业重金招聘Python工程师标准>>> 在JavaScript中,所有的string类型(或者被称为DOMString)都是使用UTF-16编码的. MDN DOMS ...
- JavaScript中为什么string可以拥有方法?
所有文章搬运自我的个人主页:sheilasun.me 引子 我们都知道,JavaScript数据类型分两大类,基本类型(或者称原始类型)和引用类型. 基本类型的值是保存在栈内存中的简单数据段,它们是按 ...
- JavaScript中的String对象
String对象提供的方法用于处理字符串及字符. 常用的一些方法: charAt(index):返回字符串中index处的字符. indexOf(searchValue,[fromIndex] ...
- JavaScript中的string对象及方法
string对象 string对象的两种创建 var a="hello"; var b=new String("hello"); //下面是方法 //charA ...
- JavaScript 中的string 方法
创建string的方法 var str ="abc"; var str = new String("abc"); var str = String(" ...
- JavaScript中的String对象详解
1.属性 String对象最常用的属性是length,用于返回字符串对象的长度. 2.方法 CharAt(index) 返回字符串对象中指定索引号组成的字符串,位置的有效值为0到字符串的长度减1. ...
随机推荐
- C 到C++的升级
C++所有的变量都可以在需要使用时再定义. C语言中的变量都必须在作用域开始的位置定义. register 关键字请求编译器将局部变量存储于寄存器中 在C语言无法获取register 变量的地址 在C ...
- 11高级网站构建:div和span
用<div>元素把属于一个逻辑部分的元素包围起来.可以用id属性为<div>提供一个唯一的标签. <div>的作用:1.更深一步展示页面的基本逻辑结构(相当于一个逻 ...
- Flink - state管理
在Flink – Checkpoint 没有描述了整个checkpoint的流程,但是对于如何生成snapshot和恢复snapshot的过程,并没有详细描述,这里补充 StreamOperato ...
- 微信蓝牙BLE接入调试指引 硬件篇
1 平台框架简介 微信蓝牙BLE由三个模块组成,分别是蓝牙设备.微信和第三方服务器,如下图: 蓝牙设备与微信之间的通信是通过蓝牙GATT协议进行. 微信与第三方服器之间的通信是通过网络http 接口进 ...
- IHttpModule
随便写一个类继承IHttpModule 实现IHttpModule中的两个方法 Init() Dispose() public void Init(HttpApplication context) { ...
- SQL基础巩固2
日期函数 函数名称 含义 示例 GetDate 返回当前系统日期和时间,返回值类型为datetime select GETDATE()//输出当前日期 YEAR 返回指定日期的年份 YEAR('08/ ...
- mongoDb(2)聚合
1.mongodb的聚合是有专门的一个方法的.
- Invoke() 方法是 Unity3D 的一种委托机制
Invoke() 方法是 Unity3D 的一种委托机制 如: Invoke("SendMsg", 5); 它的意思是:5 秒之后调用 SendMsg() 方法: 使用 Inv ...
- 1.2 如何在visual studio 中建立C#程序
这一节简单介绍一下怎么在visual studio 2015中建立第一个C#程序,我使用的是2015版的visual studio,不同版本可能有一些差异,不过大体上是相同的,这些信息仅供新手参考,大 ...
- WCF ABC
参考文献 http://blog.sina.com.cn/s/blog_7358f8b201012pnt.html http://www.cnblogs.com/CodingPerfectWorld/ ...