javaScript 字符串方法,字符串搜索,
// 这是字符串 能够使用单引号或双引号
var mko='hello woredw'
var qwe="hello worasd"
// new 一个字符串
var ssrwa=new String("hello")
// 字符串的长度】
var mko=mko.length
// 索引的下标
console.log(qwe[0]);
// h
// 遍历
// for (var nji=0;nji<qwe.length;nji++){
// console.log(qwe[nji]);
// }
// 统计字符出现的次数
var ppp='asdddsa'
var zxc1={}
// undefined 是未定义的
for (var o1=0;o1<ppp.length;o1++){
if (zxc1[ppp[o1]]==undefined){
zxc1[ppp[o1]]=1
}else{
zxc1[ppp[o1]]++
}
}
// 字符编码
var zxc111=[]
for(var c=65;c<91;c++){
// a-z
console.log(String.fromCharCode(c));
zxc111.push(String.fromCharCode(c))
}
console.log(zxc111);
// 小写转换成大写
var kkk='hello'
console.log(kkk.toUpperCase());
// 大写转换成小写
console.log(kkk.toLowerCase());
// 截取
// substr(开始索引,结束索引)
// substring (开始索引,结束索引)
// 通过截取,首字母大写
var bh_hu='xxh'
console.log(bh_hu.substr(0,1).toUpperCase()+bh_hu.substr(1));
// split 分割为数组
var zxc='a,f,v,c'
console.log(zxc.split(","));
// 练习 空字符串 分割
var zxc_1='a f v b'
console.log(zxc_1.split(' '));
// 查找字符串中的字符串 indexOf() 方法返回字符串中指定文本首次出现的索引(位置)
// 第一次出现在
// 如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1。
var bnm='The full name of China is the People s Republic of China full'
console.log(bnm.indexOf('full')); // 4
console.log(bnm.indexOf('name'));// 9
//lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引:
console.log(bnm.lastIndexOf('full')); //58
// concat() 连接两个或多个字符串:
var zxc='hello'
var mok='woredf'
var text3=zxc.concat('',mok)
console.log(text3);
// String.trim()
// trim() 方法删除字符串两端的空白符:
var zxc111=' hello eofdd '
console.log(zxc111);
console.log(zxc111.trim());
//小案例 模糊查询 找出有a的
var lll=['aa','bbb','cac','www','aq',]
for (var n=0;n<lll.length;n++){
if (lll[n].indexOf('a')!=-1){
console.log(lll[n]);
};
}
// 也可以有数组的回调函数
var xxh=['手机','手套','手柄','电脑','电视']
var name=prompt('输入')
var lp_io=xxh.filter(function(item){
return item.indexOf(name)!=-1
})
document.write(lp_io.join(''))
// JSON 字符串 转换成 对象
var vv='{"name":"xxh","age":19}'
var obj=JSON.parse(vv)
console.log(obj);
// {name: 'xxh', age: 19}

浙公网安备 33010602011771号