JavaScript [string methods (charAt(), concat(), slice(), substring(), substr(), indexOf()/lastIndexOf(), trim())] (5)

 

Table of contents

string

string transcoding

String method _charAt()

String method _concat()

String method _slice()

String method _substring()

String method _substr()

String methods _indexOf()/lastIndexOf()

String method _trim()

String method _toLowerCase()/toUpperCase()

String methods _search()/replace()

String method _split()


string

A string is zero or more characters lined up together, enclosed in single or double quotes

'xiaotong'
"xiaotong"

 Inside single-quoted strings, double quotes can be used. Inside double-quoted strings, you can use single quotes

'key = "value"'
"It's a long itxiaotong"

If you want to use single quotes inside a single quote string, you must add a backslash in front of the inner single quotes to escape. Double quotes are used inside double quoted strings, as well

'Did she say \'Hello\'?'
// "Did she say 'Hello'?"

"Did she say \"Hello\"?"
// "Did she say "Hello"?"

Kind tips

The string can only be written in one line by default, if it is divided into multiple lines, an error will be reported

If a long string must be broken into multiple lines, you can use a backslash at the end of each line

var longString = 'Long \
long \
string';
longString
// "Long long long string"

 escape

The backslash (\) has a special meaning in the string and is used to represent some special characters, so it is also called an escape character

\0 :null(\u0000)
\b :后退键(\u0008)
\f :换页符(\u000C)
\n :换行符(\u000A)
\r :回车键(\u000D)
\t :制表符(\u0009)
\v :垂直制表符(\u000B)
' :单引号(\u0027)
" :双引号(\u0022)
\ :反斜杠(\u005C)

length property

The length attribute returns the length of the string, which cannot be changed

var s = 'itxiaotong';
s.length // 9

string transcoding

Base64 transcoding 

The so-called Base64 is an encoding method that can convert any value into printable characters composed of 64 characters from 0 to 9, A to Z, az, + and /. The main purpose of using it is not to encrypt, but to simplify the processing of the program without special characters

JavaScript natively provides two methods related to Base64

btoa(): Convert any value to Base64 encoding

atob(): Convert Base64 encoding to the original value

var string = 'Hello World!';
btoa(string) // "SGVsbG8gV29ybGQh"
atob('SGVsbG8gV29ybGQh') // "Hello World!"

 Kind tips

Chinese cannot be processed

If you have to deal with Chinese, you need to add two methods

encodeURIComponent()
decodeURIComponent()
function b64Encode(str) {
  return btoa(encodeURIComponent(str));
}
function b64Decode(str) {
  return decodeURIComponent(atob(str));
}
b64Encode('你好') // "JUU0JUJEJUEwJUU1JUE1JUJE"
b64Decode('JUU0JUJEJUEwJUU1JUE1JUJE') // "你好"

String method _charAt()

The charAt method returns the character at the specified position, and the parameters are numbered  from 0

var s = new String('itxiaotong');

s.charAt(1) // "t"
s.charAt(s.length - 1) // "n"

If the argument is negative, or greater than or equal to the length of the string, charAt returns an empty string

'itxiaotong'.charAt(-1) // ""
'itxiaotong'.charAt(9) // ""

String method _concat()

The concat method is used to concatenate two strings and return a new string without changing the original string

var s1 = 'itxiaotong';
var s2 = 'txc';
s1.concat(s2) // "itxiaotongtxc"
s1 // "itxiaotong"

This method can accept multiple parameters

'txc'.concat('itxiaotong', 'bjtxc') //"txcitxiaotongbjtxc"

If the parameter is not a string, the concat method will convert it to a string first, and then connect

var one = 1;
var two = 2;
var three = '3';

''.concat(one, two, three) // "123"

String method _slice()

The slice method is used to extract a substring from the original string and return it without changing the original string. Its first parameter is the start position of the substring, and the second parameter is the end position of the substring (excluding this position)

'itxiaotong'.slice(0, 4) // "it"

 If the second parameter is omitted, it means that the substring reaches the end of the original string

'itxiaotong'.slice(2) // "xiaotong"

If the parameter is a negative value, it indicates the countdown position from the end, that is, the negative value plus the length of the string

'itxiaotong'.slice(-7) // "xiaotong"
'itxiaotong'.slice(0, -7) // "it"
'itxiaotong'.slice(-2, -1) // "n"

If the first parameter is greater than the second parameter, the slice method returns an empty string

'itxiaotong'.slice(2, 1) // ""

String method _substring()

The substring method is used to extract a substring from the original string and return it without changing the original string, which is very similar to the slice method. Its first parameter indicates the start position of the substring, and the second position indicates the end position (the returned result does not include this position)

'itxiaotong'.substring(0, 2) // "it"

 If the second parameter is omitted, it means that the substring goes to the end of the original string

'itxiaotong'.substring(2) // "xiaotong"

If the first parameter is greater than the second parameter, the substring method will automatically replace the positions of the two parameters

'itxiaotong'.substring(9, 2) // "xiaotong"
// 等同于
'itxiaotong'.substring(2, 9) // "xiaotong"

 If the parameter is a negative number, the substring method will automatically convert the negative number to 0

'itxiaotong'.substring(-3) // "itxiaotong"
'itxiaotong'.substring(2, -3) // "it"

Since these rules are counterintuitive, the substring method is deprecated in favor of slice

String method _substr()

The substr method is used to extract a substring from the original string and return it without changing the original string, which is the same as the slice and substring methods

The first parameter of the substr method is the starting position of the substring (counting from 0), and the second parameter is the length of the substring

'itxiaotong'.substr(2, 7); // xiaotong

If the second parameter is omitted, it means that the substring goes to the end of the original string

'itxiaotong'.substr(2) // "xiaotong"

If the first parameter is a negative number, it indicates the character position of the reciprocal calculation. If the second parameter is negative, it will be automatically converted to 0, so an empty string will be returned

'itxiaotong'.substr(-7) // "xiaotong"
'itxiaotong'.substr(4, -1) // ""

String methods _indexOf()/lastIndexOf()

The indexOf method is used to determine the first occurrence of a string in another string, and the returned result is the position where the match begins. If -1 is returned , it means there is no match

'hello world'.indexOf('o') // 4
'itxiaotong'.indexOf('txc') // -1

The indexOf method can also accept a second parameter, indicating that it will start matching backwards from this position

'hello world'.indexOf('o', 6) // 7

The usage of the lastIndexOf method is the same as that of the indexOf method. The main difference is that lastIndexOf matches from the end, while indexOf matches from the head.

'itxiaotong'.lastIndexOf('z') // 5

String method _trim()

The trim method is used to remove the spaces at both ends of the string and return a new string without changing the original string

' hello world '.trim()
// "hello world"

This method removes not only spaces, but also tabs ( \t , \v ), newlines ( \n ) and carriage returns ( \r )

'\r\nitxiaotong \t'.trim() // 'itxiaotong'

 ES6 extension methods, trimEnd() and trimStart() methods

"   itxiaotong   ".trimEnd(); //   itxiaotong
"   itxiaotong   ".trimStart(); // itxiaotong 

 

String method _toLowerCase()/toUpperCase()

The toLowerCase method is used to convert a string to all lowercase, and toUpperCase is to convert all to uppercase. They both return a new string without changing the original string

'Hello World'.toLowerCase()
// "hello world"

'Hello World'.toUpperCase()
// "HELLO WORLD"

Change the first letter of a string to uppercase

itxiaotong'.charAt(0).toUpperCase() +
'itxiaotong'.substr(1);

String methods _search()/replace()

The search method determines whether the original string matches a substring, and returns the first position of the match. Returns -1 if no match is found

'itxiaotong,txc'.search('xia') // 2

The replace method is used to replace the matched substring

'txcxiaotong'.replace('txc', 'it') // "itxiaotong"

String method _split()

The split method splits the string according to the given rules and returns an array consisting of the split substrings

'it|txc|xiaotong'.split('|') // ["it", "txc","xiaotong"]

If the split rule is an empty string, the members of the returned array are each character of the original string.

'a|b|c'.split('') // ["a", "|", "b","|","c"]

 If the argument is omitted, the only member of the returned array is the original string

'it|txc|bz'.split() // [it|txc|bz]

The split method can also accept a second parameter, which limits the maximum number of members of the returned array.

'it|txc|bz'.split('|', 0) // []
'it|txc|bz'.split('|', 1) // ["it"]
'it|txc|bz'.split('|', 2) // ["it", "txc"]
'it|txc|bz'.split('|', 3) // ["it", "txc","bz"]
'it|txc|bz'.split('|', 4) // ["it", "txc","bz"]

Guess you like

Origin blog.csdn.net/m0_58719994/article/details/132030039