Skip to content

字符串-String

1.字符串对象

字符串本质

String对象是 JavaScript 原生提供的三个包装对象之一,用来生成字符串对象。 String对象也是JS内置对象中使用频率最高的之一。

创建字符串

在JS中创建字符串有两种方式:

  • 字面量
  • 构造函数
js
    // 字面量的形式
    var str1 = 'hello';

    // 构造函数
    var str2 = new String('world');

    console.log(str1);// 'hello'
    console.log(str2);// String {"world"}

上面两种方式创建的两个变量是不同的,第一种是基本数据类型,第二种则是引用数据(对象)类型。 它们的不同可以使用typeof运算符来输出。

js
    typeof str1;  // string
    typeof str2;  // object

字符数组

字符串其实是由一个个字符组织起来的字符数组,所以我们可以使用一些数组的属性和方法。 所以我们可以像操作数组一样的去操作字符串。

js
    var str = 'hello qianfeng!';

    // 访问字符串的长度
    console.log(str.length);// 15

    // 遍历字符串中的字符
    for( var i=0;i<str.length;i++ ){
        console.log(str[i]);
    }

ascii码和字符集

这里想要跟大家聊的其实是关于编码的知识点。 请看下面的代码:

js
    console.log('9'<1);			// false
    console.log('9'<19);		// true
    console.log('9'<'19');		// false

在上面的代码中,前两个判断很容易理解,因为在执行比较表达式的时候会把字符串'9'自动类型转换成number类型,所以结果不出意外,但是最后一个是按照什么规则来比较的呢?

ascii码

在我们第一天学习HTML的时候我们就见到了这样一个标签。

html
    <meta charset="utf-8"/>

大家应该都知道这是干嘛的,这是转换页面的字符编码的。

而第三个表达式中的比较规则就是按照字符编码表中的数据进行比较的。 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串。每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte)。也就是说,一个字节一共可以用来表示256种不同的状态,每一个状态对应一个符号,就是256个符号,从00000000到11111111。

ASCII编码查询

在表中我们可以很轻易的看出数字0:48,字符A:65,字符a:97。

js
    // 字符串之间的比较规则是
    // 挨个字符进行比较,如果第一个字符一致就往后一个字符接着比,直到比较出结果为止
    console.log('9'>'19');//true

试一试

js
    console.log('91'>'19');     
    console.log('abc'>'cba');    
    console.log('hello'>'hi');  
    console.log('wolrd'>'wow');

2.字符串常用方法

关于字符串的操作JS原生提供了非常多实用的方法:

charAt(index)

返回在指定索引位置的字符

js
var str1 = 'hello world!!!';

var a = str1.charAt(3);
console.log(a); // 'l'

charCodeAt(index)

返回在指定的位置的字符的 Unicode 编码。

js
var b = str1.charCodeAt(0);
console.log(b);  // 104

var c = str1.charCodeAt(5);
console.log(c);  // 32

concat()

作用:concat() 方法用于连接两个或多个字符串。

语法

js
str.concat(string1, string2, ..., stringX)

例子

js
var str1 = "Hello ";
var str2 = "world!";
var n = str1.concat(str2);

indexOf(str,index)

返回某个指定的字符串值在字符串中首次出现的位置

  • 第一个参数为字符串值,如存在返回下标,不存在返回**-1**

  • 第二个参数为索引(可忽略不写),表示从字符串开始搜索的索引

js
console.log(str1.indexOf('l'));  // 2
console.log(str1.indexOf('l',4));  // 9
console.log(str1.indexOf('a'));  // -1

lastIndexOf(str,index)

返回某个指定的字符串值在字符串中最后出现的位置

  • 第一个参数为字符串值,如存在返回下标,不存在返回**-1**

  • 第二个参数为索引(可忽略不写),表示从字符串开始搜索的索引

js
console.log(str1.lastIndexOf('l'));  // 9
console.log(str1.lastIndexOf('l',7));  // 3
console.log(str1.lastIndexOf('z'));  // -1

toLowerCase()

  • 把字符串转化为小写

toUpperCase()

  • 把字符串转化为大写
js
var str2 = 'NihaoMEIWohAoshUaI';
console.log(str2);
// toLowerCase()    把字符串转化为小写
console.log(str2.toLowerCase());  // nihaomeiwohaoshuai

// toUpperCase()    把字符串转化为大写
console.log(str2.toUpperCase());  // NIHAOMEIWOHAOSHUAI

substr(start,length)

从起始索引号提取字符串中指定数目的字符

  • 第一个参数为开始提取字符串的索引
  • 第二个参数为所需要提取字符串的长度(可忽略不写,则从索引开始提取到字符串结束)
js
const str='123456'

// 一个参数,截取到最后
console.log(str.substr(1))  // 23456

// 两个参数,截取从第一个下标开始,第二个参数的长度的内容
console.log(str.substr(2,4))  // 3456

substring(start,stop)

提取字符串中两个指定的索引号之间的字符

  • 第一个参数为开始提取字符串的索引
  • 第二个参数为结束提取字符串的索引(不包含此索引),(可忽略不写,则从索引开始提取到字符串结束)
js
const str='123456'

// 一个参数,从开始下标一直截取到末尾
console.log(str.substring(0))       // 123456

// 两个参数,截取下标中间内容
console.log(str.substring(0,3))  // 123

split()

通过指定字符分隔字符串返回数组

  • 不穿参数:将整个字符串转换成数组的第一项元素
  • 传入一个空字符串:将字符串的每一个字符都转换成数组的一项元素,返回数据
  • 传入指定字符:将字符串按照指定字符分割成数组,且字符串中的指定字符被删去
js
const str='abc123'

// 不传入参数
console.log(str.split());      // [ 'abc123' ]
// 传入空字符串
console.log(str.split(''));     // [ 'a', 'b', 'c', '1', '2', '3' ]
// 传入指定字符
console.log(str.split('1'))     // [ 'abc', '23' ]

replace('xx','yy')

替换字符串

  • 第一个参数:被替换的字符
  • 第二个参数:替换成的字符
js
const str='aabbcc'

console.log(str.replace('aa',11)) 	// 11bbcc	
console.log(str.replace('aa','你好'))	// 你好bbcc

replaceAll('x','y')

替换所有指定的字符串,可用来去除空格

  • 第一个参数:被替换的字符
  • 第二个参数:替换成的字符
js
const str='a1a2a3a4a5a6a7a8a9'

console.log(str.replaceAll('a',''))  // 123456789

const str2=' a  a a a  a  '
console.log(str2.replaceAll(' ',''))  // aaaaa

repeat(num)

复制字符串指定次数,并将它们连接在一起返回,空格也算

接收一个数值型参数,作为重复的次数

js
const str='abc123'

console.log(str.repeat())	// 
console.log(str.repeat(1))	// abc123
console.log(str.repeat(2))	// abc123abc123

trim()

去除字符串两端的空白字符

js
const str='    abc 123  '

console.log(str.trim())  //abc 123

slice()

截取字符串,使用方法和字符串的substring方法基本一样

js
const str='abcd1234'

console.log(str.slice(1))  // bcd1234
console.log(str.slice(1,3))  // bc
console.log(str.slice(1,-6))  // b

String.fromCharCode()

可接受一个指定的 Unicode 值,然后返回一个字符串

js
console.log(String.fromCharCode(69));  /E

MIT Licensed