JavaScript basics -- string

Posted by Richter on Wed, 22 Sep 2021 06:34:59 +0200

1. Talk at the beginning

Before you start, you need to understand the following questions:

  • string is a basic data type and has no properties and methods
  • We can call properties or methods after str. in fact, the most important thing is the credit of the basic wrapper type.
  • There are three basic packing types: String, Boolean and Number.

When we use let str="123;str.XX(), the following happens:

  • Create an instance of String type;
  • Call the specified method on the instance;
  • Destroy this instance
    In other words, the package will be destroyed immediately if it loses its use value.

This also leads to the following problem:
Why can't properties and methods be defined on basic types?
Whenever the values of these three basic types are read, the corresponding wrapper type instance will be created in the background. This instance will call the specified method and will be destroyed after calling. This short life cycle determines that we cannot add custom properties and methods for basic types.

2. Officially

First, define a basic type of string.
Note: when using the str.XXX() method, because a String type will be generated, the value of STR itself will not be changed, so the value of STR is always hahaha, how are you

	let str=" Ha ha ha, how are you?     ";
    console.log('str The data type is',typeof str);
  • Create an anchor label
let str1=str.anchor("123456")
console.log(str1);
  • Create a label to increase the font size. str is the text between elements, which has been discarded
let str2=str.big();
console.log(str2);
  • Create a label to flash text and discard it because it does not meet accessibility standards
let str3=str.blink();
console.log(str3);
  • charAt() is used to find the index element at the corresponding position in the string, and the subscript starts from 0
let str4=str.charAt(3);
console.log(str4);
  • charCodeAt() converts the index element of the corresponding position into charCodeAt(). The method returns an integer between 0 and 65535, representing the UTF-16 code unit at the given index
let str5=str.charCodeAt(0);
		console.log(str5);
  • codePointAt() represents a Unicode encoding unit from 0 to 1114111
let str6=str.codePointAt(0);
		console.log(str6);
  • concat() connects multiple strings and supports multiple parameter options
let str7=str.concat('123','456');
  • endsWith() is used to determine whether the string ends with "XX". The second parameter can specify the end position of the search.
let str8=str.endsWith('Ha',4);
console.log(str8);
  • fixed() causes strings to be displayed at fixed intervals and returns labels
let str9=str.fixed();
		console.log(str9);
  • fontcolor() defines the color displayed by the font, and returns the label with the color attribute
let str10=str.fontcolor('red');
		console.log(str10);
  • fontsize() defines the size of the font display, and returns the label with the size attribute
let str11=str.fontsize(20);
console.log(str11);
  • includes is used to judge whether a string contains substrings. It is case sensitive and returns true or false
    //First parameter: substring to search
    //The second parameter: which index to start retrieval from. The default is 0
let str12=str.includes("Ha you",1);
		console.log(str12);
  • indexOf() is used to find the first occurrence position of the substring, and you can specify the retrieval position.
let str13=str.indexOf("Ha")
console.log(str13);
  • lastIndexOf() is used to find the last occurrence position of the substring. You can specify the retrieval position.
let str14=str.lastIndexOf("Ha");
	console.log(str14);
  • localeCompare is used to compare the sequence of strings
let str15=str.localeCompare("123");
	console.log(str15);
  • match() supports regular expressions. The parameter must be a regular expression object RegExp, and the return value is an array, which is the matched string
let str16=str.match(/Ha/g);
	console.log(str16);
  • matchAll() supports regular expressions, and the return value is an array,
let str17=str.matchAll("Ha");
	console.log(str17);
	
  • normalize normalizes and returns a node
let str18=str.normalize();
	console.log(str18);
  • padEnd() string completion method, which completes at the end of the string
let str19=str.padEnd(12,0);
	console.log(str19);
	
  • padStart() completes the string before the string
let str20=str.padStart(12,0);
	console.log(str20);
  • repeat is equivalent to copying a string. The parameter specifies the number of copies of the specified assignment.
let str21=str.repeat(3);
	console.log(str21);
  • Replace is mainly used to replace strings. The work includes two parts:
    (1) Use regular expressions to find strings that satisfy the pattern
    (2) Replace the old value with the new value
let str22=str.replace(/Ha/g,'Hee');
	console.log(str22);
  • search() searches for substrings through regular expressions
let str24=str.search(/Ha/g);
	console.log(str24);
  • slice() is mainly used to slice the string according to the index, excluding the elements at the tail index.
let str25=str.slice(3,4)
	console.log(str25);
  • split splits the original string according to the separator to get a new array
let str26=str.split("");
	console.log(str26);
  • startsWith() is to find whether the string starts with a specific element – "false" true
let str27=str.startsWith("Ha");
	console.log(str27);
  • sub() returns a label element that represents a subscript and sup is a superscript
let str28=str.sup();
	console.log(str28);
  • substr(), intercepting substrings from 0, intercepting 3 lengths.
let str29=str.substr(1,3);
	console.log(str29);
  • substring(), intercepts substrings in the [1,3) interval, excluding elements with tail subscripts,
    //What's the difference with slice?
    let str30=str.substring(1,3);
    console.log(str30);

  • toLowerCase() is all converted to lowercase for representation

let str31=str.toLowerCase()
	console.log(str31);
  • trim() removes the space characters contained at the beginning and end of the string.
let str32=str.trim();
	console.log(str32);
  • trim() removes the space characters contained at the beginning and end of the string.
let str33=str.trimEnd();
		console.log(str33);

3. Write at the end

Many of these methods of string have been abandoned. For example, sub() sup() big(), these methods will return an HTML tag element. These methods have been gradually abolished, and it is best not to use them.

Note: no matter what the operation is, the original string will not be changed.

Topics: Javascript