The use of String objects in Js

        1. What is a String object

StringThe global object is a constructor for a string or a sequence of characters. A string is used to store a series of characters like "John Doe", a string can use single quotes or double quotes.

        2. How to declare a String object

Generally, the string object is declared in the following ways. Generally, you can use the first one, the second one can convert other types of objects to String type, and the third one gets a string object.

let str1 = "hello world"
let str2 = String(123)
let str3 = new String(456)

        3. Common methods of Date

                length

var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
console.log(str.length)
// 26
//在字符串中空格也算一个长度

                indexOf() 

let str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

let index1 = str.indexOf('A')
let index2 = str.indexOf('Z')
let index3 = str.indexOf('a')

console.log(index1)        //0
console.log(index2)        //25
console.log(index3)        //-1
//这里的索引也是从零开始,最后一位的索引为str.length-1. 查找的元素不存在则返回-1

                match()

var str = "Hello world!"
let result1 = str.match("Hello")
let result2 = str.match("JS")
console.log(result1)    //"Hello"
console.log(result2)    //null

//用来查询是否含有该字符串,含有就返回该字符串

                charAt()

let str = "Hello world!"
let result1 = str.charAt(1) 
let result2 = str.charAt(0) 

console.log(result1)        // "e"
console.log(result2)        // "h"

//不给索引的话,默认为0.如果给的索引超过了索引的最大值,或者小于0,那么都会返回空的字符串

                concat()

let str1 = "Hello"
let str2 = " world!"

let str = str1.concat(str2)

console.log(str)
//"Hello world!"

//会使连个字符串合并称为一个新的字符串

"".concat(null)  // "null"
"".concat(true)  // "true"
"".concat(4, 5)  // "45"

//也可以使用空的字符串直接使用concat方法。

                replace() 

let str = "Hello world!"
let str1 = str.replace("world","javascript")

console.log(str1)
//"Hello javascript"

//需要两个参数一个是需要替换的值(可以是字符串或者是一个正则表达式),第二个参数是需要被替换成的值(可以是一个字符串或者是一个每次匹配都要调用的回调函数)

                includes()

let str1 = "Hello World"
let str2 = "Hello"
let str3 = "world"

let result1 = str1.includes(str2)
let result2 = str1.includes(str3)

console.log(result1)        //true
console.log(result2)        //false

//判断一个字符串中是否含有另一个字符串        str.includes(searchString[, position])
//[, position]表示需要检索的字符串,在str中的起始位置,默认为0,可选填。

                repeat()

let str = 'abc'
let result1 = str.repeat(-1)        // RangeError: repeat count must be positive and less than inifinity
let result1 = str.repeat(0)        //""
let result1 = str.repeat(1)        //"abc"
let result1 = str.repeat(2)        //"abcabc"        
let result1 = str.repeat(2.5)          //"abcabc"

//表示该字符串以一定数量链接起来形成新的字符串。参数为负值或者为1/0这种不符条件的数时,直接报错。
//当参数以小数形式出现时,那么默认为小于该参数的最大整数值。

                search()

const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const regex = /[^\w\s]/g;

console.log(paragraph.search(regex));
// 输出: 43

console.log(paragraph[paragraph.search(regex)]);
// 输出: "."

//该方法用于返回查找元素对象的首字母位置。也就是说在找到复合要求的元素对象后返回第一个字母的索引。

                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"

//str.slice(beginIndex[, endIndex]) beginIndex表示开始的索引值,[, endIndex]表示结束的索引值,可选填。如果有该值不写,就默认提取到字符串尾。如果beginIndex为负值,那么就表示负值到句尾的字符串。
//该属性的索引值可以负数,但是在填写负数时要注意,一样要满足字符串从左往右的顺序。

                substr()

const str = 'Mozilla';

console.log(str.substring(1, 3));
// 输出: "oz"

console.log(str.substring(2));
// 输出: "zilla"

//该方法包含两个参数,第一个为开始位置的索引,第二个参数为结尾的索引。截取的字符串包括开始位置的索引,不包括结束的索引。
//如果只填一个值,那么就表示该值后到字符串结尾的所有字符。
//!!!!这里的索引不能为负

                split()

var str="How are you doing today?";

const words = str.split(' ');
console.log(words[3]);
// 输出: "doing"    [How,are,you,doing,today?]
const chars = str.split('');
console.log(chars[8]);
// 输出: "y"   空格也算一个元素    [H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?]
const strCopy = str.split();
console.log(strCopy);
// 输出: Array["The quick brown fox jumps over the lazy dog."]
//splice方法使用指定的分隔符字符串将一个String对象分割成子字符串数组,以一个指定的分割字串来决定每个拆分的位置  如果不填条件,或者填的条件不存在,那么就是一整个字符串是一个数组

                trim()

const greeting = '   Hello world!   ';

console.log(greeting);
// 输出: "   Hello world!   ";

console.log(greeting.trim());
// 输出: "Hello world!";

//该trim()方法从字符串的两端删除空格并返回一个新字符串,而不修改原始字符串。此上下文中的空白是所有空白字符(空格、制表符、不间断空格等)和所有行终止符(LF、CR 等)。

               

                Commonly used methods are listed here.

Guess you like

Origin blog.csdn.net/Jsy_997/article/details/124404091