字符串

字符串

字符串的定义:定义一个变量,然后直接将字符串赋值给它。

var str = “abc123”;

1)访问字符串里面的字符:

a.数组方式: str[i]

b.字符串特有的方式:charAt(i)


(2)查找:

a.数组方式:indexOf

b.字符串方式:search()方法:传入参数(要搜索的子串),返回子串的第一个字符在父串的下标。

注:这个方法对大小写敏感

search方法的功能比indexOf更强大,它可以使用正则表达式

var str = "hello world";
alert(str2 = str.search("world"));


(3)取子串

a.数组方式:slice()

b.字符串特有的方法:

  substr(start , length): 传入参数(第一个参数:开始截取的下标,如果没有第二个参数,它就一直截取到最后)(第二个参数:截取的长度)

var str = "hello world";
alert(str2 = str.search("world"));
//    alert(str[0]);

substring()传入两个参数,第一格参数:开始截取的下标;第二个参数:截取结束的下标。

var str = "hello world";
alert(str.substring(0,3));//hel

 

toUpperCase(): 小写字母转换成大写字母的方法。

var str = prompt("请输入一个英文字符串");
alert(str2 = str.toUpperCase());

 

totoLowerCase(): 大写字母转换成小写字母的方法。

var str = prompt("请输入一个英文字符串");
alert(str2 = str.toLowerCase());

 

trim():去除字符串头和尾的空格符,节省空间

var str = prompt("            请输入一个英文字符串             ");
str = str.trim();//去除字符串头和尾的空格,节省空间
alert(str);


(4)替换

a.数组方式:splice

b.字符串方式:replace(): 替换。传入的参数(第一个参数:被替换的子串)(第二个参数:新的子串)

var str = "hello world";
str = str.replace("world","china");
alert(str);//hello china

 

var str = "hello world china";
str = str.replace(" ",",");
alert(str);//hello,world china


split(x): 字符串的分割。用字符串”x”进行分割,分割之后是一个数组

var a = "a b c";
var result = a.split(" ");
alert(result);//"a","b","c"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值