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. ...
随机推荐
- MVC应用程序中,怎样控制与复制相同的功能
先看此篇<MVC程序实现Autocomplete功能> http://www.cnblogs.com/insus/p/3546255.html 它是实现使用jQuery实现文本框输入文字, ...
- Wordpress 标题设置
使用标题格式:首页(网站标题 - 网站副标题),其他页面(页面标题 | 网站标题) 在后台找到头部文件head.php <?php wp_title('|', true, 'right'); e ...
- libgdx actor 透明问题
遇到一个问题,自定义的Actor在使用batch.draw时,其他Actor设置的透明影响到了这个Actor 处理方法为 在自定义Actor中 draw方法中 绘制前,加入 Color color = ...
- What’s the difference between data mining and data warehousing?
Data mining is the process of finding patterns in a given data set. These patterns can often provide ...
- JavaScript单元测试框架-Jasmine
Jasmine的开发团队来自PivotalLabs,他们一开始开发的JavaScript测试框架是JsUnit,来源于著名的JAVA测试框架JUnit.JsUnit是xUnit的JavaScript实 ...
- javascript 事件的一点感悟
javascript 冒泡事件的理解一般是这样的: 比方页面上有一个BODY里面包含一个DIV,DIV中包含一个BUTTON.在BODY,DIV,BUTTON中都有一个ONCLICK事件,在BUTTO ...
- Winform 进程、线程、treeview
进程:一个程序就是一个进程,但是也有一个程序需要多个进程来支持的情况 进程要使用的类是:Process它在命名空间:System.Diagnostics; 静态方法Start();也可以实例化对象,来 ...
- Redis linux 下安装 及扩展配置
1.首先在/usr/local/ 创建文件夹 reids Cd /usr/local/ mkdir redis 2.把redis安装包放在redis目录下面进行解压phpredis-2.2.4.tar ...
- SQL语句中:UNION与UNION ALL的区别
有些人看到题目,瞬间觉得楼主也太弱了吧,这种问题也要拿出来写,这种问题 随便会点sql 的人基本都会 Union 是会删除冗余数据 Union ALL 不会删除冗余数据 将所有的结果都展现给用户 ...
- jq 解析josn字符串
1. var obj = jQuery.parseJSON("${ruleModel.rules}"); 2. var obj = eval("("+" ...