javascript 字符串方法


//1.  str.charAt(index) //一个介于0 和字符串长度减1之间的整数
var str1 ='abcdef';
var a = str1.charAt(1);//b  0 1 1代表第二个字符
console.log(a);
//concat(str)  方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。 需要注意数组也有该方法
var a2 = 'aaaa'.concat('bbb');//"aaaabbb"(感觉不如直接用+...)
 a =['c','lalal','hhe'].concat(['1','xxx']);//["c", "lalal", "hhe", "1", "xxx"] 两个数组相加

//endsWith(str)方法用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果,都不要用它,ie没实现
a= 'xxx.jpg'.endsWith('jpg');//true  感觉比indexof要简洁, 但是这个属于 ECMAScript 2015(ES6)规范
a = '马萨卡'.endsWith('卡');//true
//includes(str) 方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回true或false。
a = 'lalal'.includes('laa');//false  这个方法感觉没啥用,可以用indexof替换


//indexOf() 方法返回调用  String 对象中第一次出现的指定值的索引,开始在 fromIndex进行搜索。如果未找到该值,则返回-1。
//找到了就返回该第一次出现的位置,没找到就返回-1
//str.indexOf(searchValue[, fromIndex])  指定数字,从该数字往右边查询  0开始算,找到一个为准
a ='https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array'.indexOf('JavaScript');//45
a ='https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array'.indexOf('https');//0
a ='https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array'.indexOf('tps');//2
a ='https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array'.indexOf('tpss');//-1

//str.lastIndexOf(searchValue[, fromIndex]) //指定数字,从该数字往左边查询,0开始算 5是第六个字符,找到一个人为准
a ='https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array'.indexOf('/');//6
a ='https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array'.lastIndexOf('/');//80
a ='https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array'.lastIndexOf('/',5);//-1
a ='https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array'.lastIndexOf(':',5);//5


//  str.match(regexp);  当一个字符串与一个正则表达式匹配时, match()方法检索匹配项。



//str.replace(regexp|substr, newSubStr|function) replace() 方法返回一个由替换值替换一些或所有匹配的模式后的新字符串。
//模式可以是一个字符串或者一个正则表达式, 替换值可以是一个字符串或者一个每次匹配都要调用的函数。
//注意:原字符串不会改变。
var x1 = 'xxxaxxx'
a = x1.replace('a','b');//xxxbxxx
console.log(x1);//xxxaxxx

//str.search(regexp)  search() 方法执行正则表达式和 String对象之间的一个搜索匹配。 匹配正则,感觉正则需要联系,不应强记


//str.slice(beginSlice[, endSlice]) 提取字符串,指定开始和结束; 
//特点:包含开始不包含结束,从0开始算  第二个参数为负数,表示倒数第几个,-1表示倒数第一个(注意这里不是从0开始算了),第二个参数不写,表示截取完
a = '0123456789'.slice(0,2);  //01  索引 0 1 的值
a = '0123456789'.slice(2,2);  // 空  没截取到

a = '0123456789'.slice(2,-1);  // 2345678  索引2到  倒数第一个
a = '0123456789'.slice(4,);  // 456789 截取完
a = '0123456789'.slice(-2);  //  89  倒数第二到完,
a = '0123456789'.slice(-4,-3);  //  6  倒数第4到倒数第3 


//str.split([separator[, limit]])  分割为数组.limit表示最大的数组长度
//就是把指定字符串之间的内容都提取出来,表示数组格式,所以
a = '/https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/split/'.split('/')
//["", "https:", "", "developer.mozilla.org", "zh-CN", "docs", "Web", "JavaScript", "Reference", "Global_Objects", "String", "split", ""]

//str.substr(start[, length])  方法返回一个字符串中从指定位置开始到指定字符数的字符。
//从指定位置,截取指定个数的字符串, 第二个参数不写,表示截取完,负数表示倒数第几个开始截取
a = 'abcdefghtjk'.substr(2,5);  // cdefg
a = 'abcdefghtjk'.substr(-1);  // k
a = 'abcdefghtjk'.substr(1);  // bcdefghtjk

//str.substring(indexStart[, indexEnd]) 指定开始和结束的字符串,不包含结尾
//slice() 比 substring() 要灵活一些,因为它允许使用负数作为参数。因此可以尽量用slice()
a = '0123456789'.substring(1,2);//1
a = '0123456789'.substring(-2);//0123456789  不支持负数以及其他值()



//转大小写 无需多言
//String.prototype.toLowerCase()
//String.prototype.toUpperCase()

//str.trim()  不影响原来的字符串,返回新的字符串
a = '  a  v  c    '.trim();//a  v  c  不会把里面的空格也去掉



//str.valueOf()   Object也有valueOf 方法, valueOf主要把对象转换成一个基本数据的值  
//String 对象的 valueOf 方法返回一个String对象的原始值。该值等同于String.prototype.toString()。


//JavaScript 调用 Object.valueOf() 方法用来把对象转换成原始类型的值(数值、字符串和布尔值)。 
//你很少需要自己调用此函数;当遇到一种需要转换成一个原始值情况时候, JavaScript 会自动调用此函数。


// var strFullPath = window.document.location.href;//   http://localhost:8080/oa/index.do
// var strPath = window.document.location.pathname;//   /oa/index.do
// var pos = strFullPath.indexOf(strPath);
// var prePath = strFullPath.substring(0, pos);  //   http://localhost:8080
// var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);  //  /oa
// var url = prePath + postPath;  //  http://localhost:8080/oa

var fullPath = location.href;
var pathname = location.pathname;
a= '/oa/xx/index.do'.slice(1).indexOf('/')   // 2 去掉前面第一个'/'  得到第二个'/'的位置
a+1;
a = '/oa/xx/index.do'.slice(0,a+1); //oa
   // 得到/oa

   //location的值获取  一定要精通字符串的使用
// location.href
// "https://developer.mozilla.org/zh-CN/docs/Web/API/Location"

// location.host
// "developer.mozilla.org"


// location.pathname
// "/zh-CN/docs/Web/API/Location"


console.log(a);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值