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. ...
随机推荐
- ios-改变button四个角的弧度
-(void)createTitleView{ UIView * backview = [[UIView alloc]init]; backview.frame =CGRectMake(87*kHei ...
- IAR EW8051-8.10.4安装及破解方法
第一步:获取破解license 1: 点击桌面左下角“开始”按钮,找到cmd.exe,右键创建cmd.exe 快捷方式到桌面: ————如果是windows7 ,请右键点击cmd.exe 快捷图标,点 ...
- websocket和swoole
<head></head><body><script type="text/javascript">var sock = null; ...
- 学习mysql
一 概述 1.什么是数据库 数据库就是数据的仓库. mysql是对数据库进行存储和指令操作的软件.这类软件成为数据管理系统Database Management System. 2.mysql的安装和 ...
- IEnumerable、GetEnumerator、IEnumerator的理解
概念文字性的东西,我们就不说了,这里我们来点具体的实例第呀: 实例一: using System; using System.Collections; using System.Collections ...
- RDIFramework.NET ━ 9.13 系统日志与系统异常管理 ━ Web部分
RDIFramework.NET ━ .NET快速信息化系统开发框架 9.13 系统日志与系统异常管理 -Web部分 一个软件在投入运行时不可能没有任何异常,在软件发生异常时及时的记录下来,也好我 ...
- Cocos2d-x PluginX (二)增加新的Plugin
创建Plugin目录 第一步,在plugin/plugins下,目录需要严格按照如下规范实现: plugin/plugins/alipay/proj.android /proj.ios 因为publi ...
- 如何学习Python
[整理]如何学习Python + 如何有效利用Python有关的网络资源 + 如何利用Python自带手册(Python Manual) http://www.crifan.com/howto_lea ...
- android开发中scrollview添加自定义view的滑动显示问题
最近做了一个实战用到自定义view,由于view比屏幕大所以想放到scrollview中,如下程序.发现不显示.于是对scrollview进行了研究. <LinearLayout xmlns:a ...
- drop delete truncate 区别
http://jingyan.baidu.com/article/8275fc8693e11846a03cf696.html