String

# String

# 字符串模板

// 字符串拼接
const a = 1;
const b = 20;
const c = 'javascript'

// es5
const str = 'my age is ' + (a + b) + ' i love '+ c; // my age is 21 i love javascript

// es6
const str = `my age is ${a + b} i love ${c}`; // my age is 21 i love javascript
// 通过方法控制模板返回
function price(str,type){
  const price1 = 10;
  const price2 = 20
  let showTit = '' ;
  if(type == 1){
    showTit = '购买的单价为: ¥'+ price2
  }else{
    showTit = '购买的批发价为: ¥'+ price1
  }
  return `${str}${showTit}`
}
price`您此次${1}` // 您此次,购买的单价为: ¥20
// 字符串换行
const str = `这是第一行
这是第二行

...
这是第 n 行
`

# 补白

for(let i = 0; i < 300; i +=10 ){
  i.toString().padStart(3,'0')  // 补齐3位 ,不足的在前面添加0
}

padStart // 在字符串前面添加
padEnd   // 在字符串后面添加

# 清除开头/结尾空格

trimStart() / trimLeft() // 清除开头空格
trimEnd()   / trimRight()  // 清除结尾空格

trim() // 清除开头和结尾的空格

'  hello  '.trim(); // 清除字符串中的开头和结尾的孔二狗

# url编码/解码

# escape 和 unescape

escape()不能直接用于URL编码,它的真正作用是返回一个字符的Unicode编码值

它的具体规则是,除了ASCII字母、数字、标点符号"@ * _ + - . /"以外,对其他所有字符进行编码。在u0000到u00ff之间的符号被转成%xx的形式,其余符号被转成%uxxxx的形式。对应的解码函数是unescape()。

还有两个点需要注意:

首先,无论网页的原始编码是什么,一旦被Javascript编码,就都变为unicode字符。也就是说,Javascipt函数的输入和输出,默认都是Unicode字符。这一点对下面两个函数也适用。 其次,escape()不对"+"编码。但是我们知道,网页在提交表单的时候,如果有空格,则会被转化为+字符。服务器处理数据的时候,会把+号处理成空格。所以,使用的时候要小心。

// escape()编码:

const time = 2018-02-09
const tile = '63元黑糖颗粒固饮'

let url = "http://localhost:8080/index.html?time="+escape(time)+"&title="+escape(tile)

// 地址栏显示结果:
"http://localhost:8080/index.html?time=2018-01-09&title=63%u5143%u9ED1%u7CD6%u9897%u7C92%u56FA%u996E"

// unescape()解码:

let url = "http://localhost:8080/index.html?time="+unescape(2018-01-09)+"&title="+unescape("63%u5143%u9ED1%u7CD6%u9897%u7C92%u56FA%u996E")

// 地址栏显示结果:
"http://localhost:8080/index.html?time=2018-01-09&title=63元黑糖颗粒固饮"

# encodeURI 和 decodeURI

encodeURI()是Javascript中真正用来对URL编码的函数。

它用于对URL的组成部分进行个别编码,除了常见的符号以外,对其他一些在网址中有特殊含义的符号"; / ? : @ & = + $ , #",也不进行编码。编码后,它输出符号的utf-8形式,并且在每个字节前加上%。 它对应的解码函数是decodeURI()

需要注意的是,它不对单引号'编码。

let url = "http://localhost:8080/index.html?time=2018-01-09&title=63元黑糖颗粒固饮"

// encodeURI()编码:
let encodeURI_url = encodeURI(url) = "http://localhost:8080/index.html?time=2018-01-09&title=63%E5%85%83%E9%BB%91%E7%B3%96%E9%A2%97%E7%B2%92%E5%9B%BA%E9%A5%AE"

// decodeURI()解码:
decodeURI(encodeURI_url)= "http://localhost:8080/index.html?time=2018-01-09&title=63元黑糖颗粒固饮"

# encodeURIComponent 和 decodeURIComponent

与encodeURI()的区别是,它用于对整个URL进行编码。"; / ? : @ & = + $ , #",这些在encodeURI()中不被编码的符号,在encodeURIComponent()中统统会被编码。 它对应的解码函数是decodeURIComponent()。

let url = "http://localhost:8080/index.html?time=2018-01-09&title=63元黑糖颗粒固饮"

// encodeURIComponent()编码:

let encodeURIComponent _url = encodeURIComponent(url)
//  http%3A%2F%2Flocalhost%3A8080%2Findex.html%3Ftime%3D2018-01-09%26title%3D63%E5%85%83%E9%BB%91%E7%B3%96%E9%A2%97%E7%B2%92%E5%9B%BA%E9%A5%AE

// decodeURIComponent()解码:

decodeURIComponent(encodeURIComponent _url)
// http://localhost:8080/index.html?time=2018-01-09&title=63元黑糖颗粒固饮

# 字符串常用方法

# toLowerCase()

// toLowerCase(): 把字符串转为小写,返回新的字符串。
var str="Hello World";
var str1=str.toLowerCase(); // hello world

# toUpperCase()

// toUpperCase(): 把字符串转为大写,返回新的字符串。
var str="hello world";
var str1=str.toUpperCase(); // HELLO WORLD

# charAt()

// charAt(): 返回指定下标位置的字符。
// 如果index不在0-str.length(不包含str.length)之间,返回空字符串。
var str="hello world";
var str1=str.charAt(6);

# charCodeAt()

// charCodeAt(): 返回指定下标位置的字符的unicode编码,这个返回值是 0 - 65535 之间的整数。
// 如果index不在0-str.length(不包含str.length)之间,返回NaN。
var str="hello world";
var str1=str.charCodeAt(1);
var str2=str.charCodeAt(-2); //NaN
console.log(str1); //101

# indexOf() / lastIndexOf()

// indexOf(): 返回某个指定的子字符串在字符串中第一次出现的位置

var str="Hello World";
var str1=str.indexOf("o");
var str2=str.indexOf("world");
var str3=str.indexOf("o",str1+1);
console.log(str1); //4 默认只找第一个关键字位置,从下标0开始查找
console.log(str2); //-1 没有找到
console.log(str3); //7


// lastIndexOf(): 返回某个指定的子字符串在字符串中最后出现的位置。
var str="Hello World";
var str1=str.lastIndexOf("o");
var str2=str.lastIndexOf("world");
var str3=str.lastIndexOf("o",str1-1);
console.log(str1); //7
console.log(str2); //-1
console.log(str3); //4

// indexOf() lastIndexOf() 方法对大小写敏感,如果子字符串没有找到,返回-1。第二个参数表示从哪个下标开始查找,没有写则默认从最后一个字符处开始查找。

# slice()

// slice(): 返回字符串中提取的子字符串。
var str="Hello World";
var str1=str.slice(2); //如果只有一个参数,则提取开始下标到结尾处的所有字符串
var str2=str.slice(2,7); //两个参数,提取下标为2,到下标为7但不包含下标为7的字符串
var str3=str.slice(-7,-2); //如果是负数,-1为字符串的最后一个字符。提取从下标-7开始到下标-2但不包含下标-2的字符串。前一个数要小于后一个数,否则返回空字符串

console.log(str1); //llo World
console.log(str2); //llo W
console.log(str3); //o Wor

# substring()

substring()用法与slice()一样,但不接受负值的参数。

// substring(): 提取字符串中介于两个指定下标之间的字符。
var str="Hello World";
var str1=str.substring(2)
var str2=str.substring(2,2);
var str3=str.substring(2,7);
console.log(str1); //llo World
console.log(str2); //如果两个参数相等,返回长度为0的空串
console.log(str3); //llo W

# split()

// split(): 把字符串分割成字符串数组。
var str="AA BB CC DD";
var string1="1:2:3:4:5";
var str1=str.split("");//如果把空字符串 ("")用作分割符,那么字符串的每个字符之间都会被分割
var str2=str.split(" "); //以空格为分隔符
var str3=str.split("",4); //4指定返回数组的最大长度
var str4=string1.split(":");
console.log(str1); // ["A", "A", " ", "B", "B", " ", "C", "C", " ", "D", "D"]
console.log(str2); //["AA" "BB" "CC" "DD"]
console.log(str3); //["A", "A", " ", "B"]
console.log(str4); // ["1", "2", "3", "4", "5"]

# replace()

// replace(): 在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
var str="hello WORLD";
var reg=/o/ig; //o为要替换的关键字,不能加引号,否则替换不生效,i忽略大小写,g表示全局查找。
var str1=str.replace(reg,"**")
console.log(str1); //hell** W**RLD

# match()

// match(): 返回所有查找的关键字内容的数组。
var str="To be or not to be";
var reg=/to/ig;
var str1=str.match(reg);
console.log(str1); //["To", "to"]
console.log(str.match("Hello")); //null