javascript数据类型之字符串

2021-03-06 14:26

阅读:553

标签:username   strlen   五个   inpu   indexof   orm   har   char   false   

字符串

1 字符串的定义

/* 凡是引号当中的内容都是字符串;?? */
"该号码可注册"
"12916316319"
""
"http://www.163.com"
‘http://www.163.com‘

2 字符串的长度 str.length

/* str.length */
"micromajo".length   //10
---
var userName = input.value;//"micromajor"
if(userName.length

3 字符串索引位置 str.charAt(index)

/* str.charAt(index) */
"micromajor".charAt(0) //m
---
var userName = input.value;//"-micromajor"
if(userName.charAt(0) == "-"){
    alert("userName can‘t start with ‘-‘");
}
---

4 字符串检索匹配:indexOf()

/* str.indexOf(searchValue[,fromIndex]) */
"micro-major".indexOf("-")  //5
"micro-major-web".indexOf("-")  //5
"micro-major".indexOf("major")  //6
"micro-major".indexOf("-")  //-1
---
var userName = input.value;//"micromajor"
if(userName.indexOf("-") == -1){
    alert("userName must contain ‘-‘");
}
---

5 字符串检索匹配:search(RegExp)

/* str.search(regexp) */
"micromajor163".search(/[0-9]/)  //10
"micromajor163".search(/[A-Z]/)  //-1
---
var userName = input.value;//"micromajor163"
if(userName.search(/[0-9]/) !=-1){
    alert("userName can‘t contain a number");
}
---

6 字符串检索匹配:match(RegExp)

/* str.match(regexp) */
"micormajor163".match(/[0-9]/)   //["1"]
"micromajor163".match(/[0-9]/g)  //["1","6","3"]
"micromajor163".match(/[A-Z]/)   //null

7 字符检索替换:replace()

/* str.replace(regexp|substr,newSubstr|function) */
"micromajor163".replace("163","##")  //"micromajor##"
"micromajor163".replace(/[0-9]/,"#")  //"micromajor#63"
"micromajor163".replace(/[0-9]/g,"#") //"micromajor##"
"micromajor163".replace(/[0-9]/g,"") //"micromajor"

8.1 str.substring(indexA,[indexB])

/* str.substring(indexA[,indexB]) */
"micromajor".substring(5,7)  //"ma"
"micromajor".substring(5)  //"major"

8.2 str.slice(startIndex,[endIndex])

/* str.slice(beginSlice[,endSlice]) */
"micromajor".slice(5,7)  //"ma"
"micromajor".slice(5)  //"major"
"micromajor".slice(1,-1)  //"icromajo"
"micormajor".slice(-3)  //"jor"

8.3 str.substr(start,[length])

/* str.substr(start[,length]) */
"micromajor".substr(5,2)  //"ma"
"micromajor".substr(5)  //"marjor"

9 字符串拆分:split

/* str.split([separator][,limit]) */
"micro major".split(" ")  //["micro","major"]
"micro major".split(" ",1)  //["micro"]
"micro2major".split(/[0-9]/)  //["micro","major"]

10 大小写转换

/* str.toLowerCase() */
"MicroMajor".toLowerCase()  //"micromajor"

/* str.toUpperCase() */
"MicroMajor".toUpperCase()  //"MICROMAJOR"

11 字符串的连接:“+”

"0571" + "-" + "88888888"  //"0571-88888888"
var area = areaInput.value;//0571
var tel = telInput.value;//88888888
var number = area + "-" + tel;//0571-88888888

12 转化为字符串型:String()

String(163)  //"163"
String(null)  //"null"

13 转义字符:“\”

"micro\"major"  //"micro"major"
"micro\\major"  //"micor\major"
"micro\tmajor"  //"micro   major"
/* to be continued

14 模板字符串(ES6新增)

//`${}`
let name = "zhangsan";
let occupation = "doctor";
let str = `he is ${name},he is a ${occupation}`;

//多行
let str = `write once,
            run anywhere`;

//${}中可以放置任意javascript表达式
var obj = {"a":1,"b":2};
var str = `the result is ${obj.a+obj.b}`;
function fn() {
    return 3;
}
var str = `the result is ${fn()}`;

15 标签模板 tagFn

标签模板,可以理解为标签函数+模板字符串

var name = "zhangsan";
var height = 1.8;
tagFn`his name is ${name},his height is ${height} meter`;

function tagFn(arr,v1,v2){
    console.log(arr);//[‘his name is‘,‘his height is‘,‘meter‘]
    console.log(v1);//zhangsan
    console.log(v2);1.8
}

16 字符串重复 str.repeat(repeatTimes)

var name1 = ‘ni‘;
var name2 = name1.repeat(3);
console.log(name1)//‘ni‘
console.log(name2)//‘ninini‘

17 判断是否包含 str.includes()

var name = "zhangsan";
name.includes(‘san‘);//true
name.includes(‘ni‘);//false
name.includes(‘z‘,1);//false 从第二个字符开始搜索

18 判断开始字符 str.startWith()

var name = "zhangsan";
name.startsWith(‘z‘);//true
name.startsWith(‘h‘);//false
name.startsWith(‘h‘,1);//true 从第二个字符开始

19 判断结束字符 str.endsWith()

var name = "zhangsan";
name.endsWith(‘z‘);false
name.endsWith(‘n‘);true
name.endsWith(‘n‘,5);false 只针对前五个字符
name.endsWith(‘g‘,5);true

20 codePointAt() - String.fromCodePoint()

var str1 = "前端";
var str2 = "??";

str1.length; //length为2
str2.length; //length为2

str1.charAt(0);  //前
str1.charAt(1);  //端

str2.charAt(0);  //‘?‘
str2.charAt(1);  //‘?‘

str.codePointAt(??);  //结果:134071
//这个数字抓换成16进制就是20bb7,对应的Unicode编码则是\u20bb7。

String.fromCodePoint(134071); //结果:"??"

21 返回原始样貌 String.raw()

即使字符串中含有转义符,它都视而不见,直接输出。

console.log(String.raw`hello\nwolrd`);
//输出:hello\nwolrd

javascript数据类型之字符串

标签:username   strlen   五个   inpu   indexof   orm   har   char   false   

原文地址:https://www.cnblogs.com/luwanlin/p/14295781.html


评论


亲,登录后才可以留言!