js字符串

js字符串讲解

1.String 定义

1.1 字面量方式

var str="abcd";

1.2 new String()方式

var str=new String();

2 String 访问和属性

2.1 String 访问

var str="abcde";
console.log(str);

str[0];

2.2 length 字符串的长度

var str="abcde";
str.length; // 5

3 String方法

3.1 concat split

concat() 合并俩个字符串

var a="abc";
var b="def";
a.concat(b);// "abcdef"

split() 通过 分隔符 切割字符串为数组

var a="abc";
split();// ["abc"]
split("");// ["a","b","c"]

3.2 charAt(index) charCodeAt fromCharCode

charAt(index) 通过 下标 获取字符串中 某一项

var str="abcdef";
// 获取 str 字符串 中 第一个字符; 
// 可以通过 数组 的方式 去获取。
console.log( str[0] );

// 也 可以通过 字符串 charAt(下标) 去获取

console.log( str.charAt(1) );

charCodeAt(index) 通过下标 获取字符串某一项 的 unicode 编码

var str="abc";
str.charCodeAt(0);	
// 1	-- 49
// A	-- 65
// a	-- 97

String.fromCharCode(unicode编码) 将 unicode 编码 转为 字符

String.fromCharCode(49); //1

3.3 indexOf lastIndexOf includes

indexOf() 查找 在 字符串中 第一次 出现的下标 。 没有-1

var str="abadea";
str.indexOf("b"); //1 

lastIndexOf() 查找 在 字符串中 最后一次 出现的下标 。 没有-1

var str="abadea";
str.lastIndexOf("a"); //5 

includes() 查找 是否包含 字符。 有 true 没有 false

var str="abadea";
str.includes("xy");// false

3.4 slice substring substr

slice(开始下标,结束下标); 剪切 包头不包尾; 用于数组、字符串

var str="abcdef";
console.log( str.slice( 1,3 ) );// bc
console.log( str.slice(-3,-1 ) )// de

substring(开始下标,结束下标); 剪切 包头不包尾; 只能用于字符串, 参数不能为负数

var str="abcdef";
console.log( str.substring( 1,3 ) );// bc
console.log( str.substring(-3,-1 ) )// de

substr(开始下标,截取个数);

var str="abcdef";
console.log( str.substr( 1,3 ) );// bcd

3.5 trim toUpperCase toLowerCase

​ trim() 去除前后空格 。返回新字符串

var str="  abc  ";
str.trim(); // "abc"

​ toUpperCase() 英文字母全部转为大写字母。返回新字符串

var str="aBc";
str.toUpperCase(); // "ABC"

​ toLowerCase() 英文字母全部转为小写字母。返回新字符串

var str="aBc";
str.toLowerCase(); // "ABC"

3.6 startsWith endsWith

var str="abd abcd abc";

console.log( str.startsWith("abc") )//开头是不是 "abc"
console.log( str.endsWith("abc") );// 结尾是不是 "abc"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值