JS 变量类型介绍(String)

# JS 变量类型介绍(String)

# 一、声明定义

使用对象形式创建字符串

let str = new String('hello');
// 打印字符串
console.log(str.toString()); // hello

使用字面量形式创建字符串

let str1 = 'hello';
let str2 = "hello";
let str3 = `hello`;

注意

以上三种方式声明字符串没有什么区别。

# 二、转义字符

有些字符无法正常显示或者有双层含义,可以使用\转义符号进行含义转换。

let content = 'I\'m teacher!'
console.log(content);

常用转义字符:

  • \t: 制表符
  • \n: 换行
  • \\: 斜杠符号

# 三、字符连接

使用+可以连接多个字符串。

let age = 22;
console.log('我今年 ' + age + ' 岁了');

当然,ES6发布之后,我们可以使用反引号连接多个字符串,并且非常的方便。

let myName = 'xiaoyang';
let myAge = 22;
console.log(`我叫${myName},我今年${myAge}`);

# 四、常用属性和方法

# 4.1 获取字符长度

console.log('xiaoyang'.length); // 8

# 4.2 转换大小写

let msg = 'hello world';
// 转换为大写
console.log(msg.toUpperCase()); // HELLO WORLD
// 转换为小写
console.log(msg.toLowerCase()); // hello world

# 4.3 字符两端移除空白

let str = '    alva-yky.top    ';
console.log(str.length); // 20
console.log(str.trim().length); // 12

# 4.4 获取单个字符

这个是从0开始计算的哈,别弄错了。

// 获取第2个字符
console.log('xiaoyang'.charAt(1)); // i

// 获取最后一个字符
console.log('xiaoyang'['xiaoyang'.length - 1]);

# 4.5 截取部分字符

有三种方式可以截取字符串:slice、substr、substring。

  • slice方法,可以接收两个参数,截取从起始索引到结束索引的字符串(不包含结束索引)。
const str = 'The quick brown fox jumps over the lazy dog.';

console.log(str.slice(31)); // "the lazy dog."

console.log(str.slice(4, 19)); // "quick brown fox"

console.log(str.slice(-4)); // "dog."

console.log(str.slice(-9, -5)); // "lazy"
  • substr方法,可以接收两个参数,从起始索引开始,截取指定长度的字符串。
const str = 'abcdefghij';

console.log("(1,2): "    + str.substr(1,2));   // (1,2): bc
console.log("(-3,2): "   + str.substr(-3,2));  // (-3,2): hi
console.log("(-3): "     + str.substr(-3));    // (-3): hij
console.log("(1): "      + str.substr(1));     // (1): bcdefghij
console.log("(-20, 2): " + str.substr(-20,2)); // (-20, 2): ab
console.log("(20, 2): "  + str.substr(20,2));  // (20, 2):
  • substring方法,可以接收两个参数,从起始索引截取到结束索引的位置(不包括结束索引)。
const str = 'xiaoyang';

console.log(str.substring(0, 3)); // 'xia'
console.log(str.substring(3,0)); // 'xia'
console.log(str.substring(4)); // 'yang'
console.log(str.substring(4, 8)); // 'yang'

# 4.6 字符串查找

常用字符串查找方法有以下几种:indexOf、lastIndexOf、search、includes

  • indexOf方法,可以接收两个参数,第一个为需要查找的字符,第二个表示开始查找的位置。
console.log('xiaoyang'.indexOf('a')); // 2
console.log('xiaoyang'.indexOf('a', 3)); // 5
  • lastIndexOf方法,顾名思义,就是反向查找,参数跟indexOf类似。
console.log('xiaoyang'.lastIndexOf('a')); // 5
console.log('xiaoyang'.lastIndexOf('a', 4)); // 2
  • search方法,用于检索字符串中指定的子字符串,也可以使用正则表达式搜索。
let str = 'xiaoyang';
console.log(str.search('xiao')); // 0
console.log(str.search('yang')); // 4
  • includes方法,用于判断字符串中是否包含指定的值,第二个参数表示开始查找的位置。
console.log('xiaoyang'.includes('a')); // true
console.log('xiaoyang'.includes('a', 3)); // true

# 4.7 替换字符串

replace方法用于字符串的替换操作。

let name = 'xiaoyang';
let newName = name.replace('a', ' ');
console.log(newName); // xi oyang

注意

默认只会替换一次,如果需要全局替换的话可以使用正则或者replaceAll方法

# 4.8 重复生成字符

可以使用repeat方法重复字符。

console.log('*'.repeat(3)); // ***

# 4.9 字符转换数组

使用split方法可以将字符转换为数组

const arr = 'xiaoyang'.split('');
console.log(arr); // ['x', 'i', 'a', 'o', 'y', 'a', 'n', 'g']
最近更新
01
JS 变量类型介绍(Array)
10-13
02
JS 变量类型介绍(Number)
10-13
03
JS 变量类型介绍(Boolean)
10-13
更多文章>