JavaScript学习之字符串
字符串
JavaScript中的String类型用于表示文本型的数据,它是由无符号整数值(16bit)作为元素而组成的集合,字符串中的每个元素在字符串中占据一个位置,第一个元素的index值是0,下一个元素的index值是1,以此类推,字符串的长度就是字符串中所含的元素个数,可以通过String字面值或者String对象两种方式创建一个字符串。获取字符串长度:length属性。
let str = "这是一个字符串字面量"; let str2 = new String("这是一个字符串对象");
console.log(str.length); // 10
String对象的方法
- indexOf() ------返回某个指定的字符串值在字符串中首次出现的位置。
- lastIndexOf() ------从后向前搜索字符串,并从起始位置(0)开始计算返回字符串最后出现的位置
let str = 'hello javaScript, hello java, hello python'; console.log("str.length==" + str.length); // str.length==42 console.log(str.indexOf("j")); // 6 console.log(str.lastIndexOf("h")); // 39
- toLowerCase() ------把字符串转换为小写
- toUpperCase() ------把字符串转换为大写
let str2 = "Hello JavaScript!"; console.log(str2.toUpperCase()); // HELLO JAVASCRIPT! console.log(str2.toLowerCase()); // hello javascript!
- endsWith() ------判断当前字符串是否是以指定的子字符串结尾
- startWith() ------判断当前字符串是否以指定的子字符串开头
- includes() ------判断字符串中是否包含指定的子字符串
let str3 = "Hello JavaScript!python!java"; console.log(str3.startsWith("Hello")); // true console.log(str3.startsWith("hello")); // false console.log(str3.endsWith("java")); //true console.log(str3.endsWith("python")); //false console.log(str3.includes("python")); //true console.log(str3.includes("typeScript")); //false
- charAt() ------返回字符串指定位置的字符
- charCodeAt() ------返回在指定的位置的字符的 Unicode 编码
let str4 = "Hello JavaScript!123456"; console.log(str4.charAt(6)); // J console.log(str4.charCodeAt(6)); // 74
- slice() ------从一个字符串提取片段并作为新字符串返回
let str5 = "hello javaScript, hello java, hello pytho"; let sliceStr5 = str5.slice(6,20); console.log(sliceStr5); // javaScript, he
- split() ------把字符串分割为字符串数组
let str6 = "java,javaScript,python,mysql,go"; let splitStr6 = str6.split(","); console.log(splitStr6); // [ 'java', 'javaScript', 'python', 'mysql', 'go' ]
- substr() ------从起始索引号提取字符串中指定数目的字符
- substring() ------提取字符串中两个指定的索引号之间的字符
let str7 = "hello javaScript, hello java, hello pytho"; console.log(str7.substr(2,10)); // llo javaSc 2为起始索引,10为提取的长度 console.log(str7.substring(6,20)); // javaScript, he
- concat() ------连接两个或更多字符串,并返回新的字符串
let hel = "hello "; let nam = "rissa "; let wel = "welcome"; let res = hel.concat(nam,wel); console.log(res); // hello rissa welcome
- replace() ------在字符串中查找匹配的子串,并替换
- replaceAll() ------在字符串中用一些字符替换另一些字符
let str8 = "Sunshine Always Comes After The Rain!"; let replaceStr8 = str8.replace("Comes","aaaa"); let replaceStr81 = str8.replaceAll("h","A"); console.log(replaceStr8); // Sunshine Always aaaa After The Rain! console.log(replaceStr81); // SunsAine Always Comes After TAe Rain!
- repeat() ------复制字符串指定次数,并将它们连接在一起返回
let str9 = "javaScript "; console.log(str9.repeat(3)); // javaScript javaScript javaScript
- search() ------检索字符串中指定的子字符串开始的位置
let str10 = "Sunshine Always Comes After The Rain!"; console.log(str10.search("shine")); // 3
- trim() ------去除字符串两端的空白
let str11 = " Sunshine always comes after the rain "; console.log(str11.length); //42 console.log(str11.trim().length); //36
- fromCharCode() ------将Unicode编码转换为字符
let n = String.fromCharCode(65); console.log(n); // A
- valueOf() ------将字符串对象转换为普通字符串
let str12 = new String("javaScript"); console.log(str12); // [String: 'javaScript'] console.log(typeof(str12)); // object let ofStr12 = str12.valueOf(); console.log(ofStr12); // javaScript console.log(typeof(ofStr12)); // string
- toString() ------返回一个字符串
let str13 = new String('Hello world'); console.log(str13); // [String: 'Hello world'] console.log(str13.toString()); // Hello world
记录学习笔记,有其他参考,如有侵权,联系删除
本文来自博客园,作者:rissa,转载请注明原文链接:https://www.cnblogs.com/rissa/p/15179720.html

浙公网安备 33010602011771号