• Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

directory

  1. CRUD
  2. increase
  3. delete
  4. change
  5. checkstr[1] indexOf, etc.Return element position | | Boolean value

A CRUD add delete change check

Read from the definition of CRUD, which is an acronym for the words increase (Create), read (Retrieve), Update (Update) and Delete (Delete) when performing computations

2 to add

Create string: Literal string object style

var str = 'abc'; // 'abc'
var str1 = new String('a'); // String {'a'}
str1.toString(); // 'a'
Copy the code

Three delete

1) Remove the STR whitespacetrim() trimStart() trimEnd()

Trim () trimStart() trimEnd() These three methods can be used to remove whitespace characters at the beginning and end of a string. Whitespace characters include Spaces, tabs, and newlines

var str = ' a b '
str.trim(); // 'a b'
str.trimStart(); // 'a b '
str.trimEnd(); //' a b'

str.trimLeft(); // 'a b '
str.trimRight(); // ' a b'
Copy the code

Four change

1) String concatenation + concat

var str = 'abc';
var str1 = str+'00'; // 'abc00'
str.concat('d'.'e'); // 'abcde'
Copy the code

2) split STR = > array

var str = 'a,b; c.d';
str.split(/[,;.]/);
// ['a', 'b', 'c', 'd']

var str = 'abcd';
str.split(' '); // ['a', 'b', 'c', 'd']
var str = 'abcd';
str.split(); // ['abcd']
Copy the code

3) toLowerCase toUpperCase

var str = 'AbCd';
str.toLowerCase(); // 'abcd'
var str = 'AbCd';
str.toUpperCase(); // 'ABCD'
Copy the code

4) Intercept stringssubstr substring slice

Substr Slice subString can be used to intercept strings without changing the original data. The second argument is optional. The first two arguments can be passed negative numbers

4.1) slice Positive sequence rules Do not change the raw data Both parameters can be negative

Used to extract a portion of a string and return the extracted portion as a new string

string.slice(start,end)
Copy the code
var str = 'abc';
var str1 = str.slice(1);
console.log(str,str1);
// 'abc' 'bc'
Copy the code
var str = 'abc';
var str1 = str.slice(-2, -1);
console.log(str,str1); // abc b
var str1 = str.slice(-2);
console.log(str,str1); // abc bc
Copy the code

The substring returned by this method includes the character at the beginning, but not the character at the end

4.2) substr

The substr method is used to extract a specified number of characters from the beginning subscript in a string. The syntax is as follows: start is a negative number. -1 is the last character in the string, -2 is the penultimate character, and so on.

string.substr(start,length)
Copy the code
var str = 'abc';
var str1 = str.substr(-1);
console.log(str,str1);
// abc c
Copy the code
4.3) substring Both parameters must be positive

The substring() method is used to extract the character of a string intermediate between two specified subscripts

string.substring(from, to)
Copy the code
var str = "abcdefg";
str.substring(1.5); //'bcde'
var str1 = str.substring(1); // 'bcdefg'
console.log(str,str1); // "abcdefg" 'bcdefg'
Copy the code

5) The string is converted to a numberparseInt()parseFloat()

The parseInt() and parseFloat() methods are both used to convert strings to numbers.

parseFloat("10.001")  / / 10.001
parseInt("10.001") / / 10
Copy the code

6) is lackingpadStart padEnd

'p'.padStart(5.'ab') // 'ababp'

'a'.padStart(5) // 'a' does not pass the second argument, default is space

'a'.padEnd(5.'i'); // 'aiiii'
Copy the code

7) repeatrepeat

'na'.repeat(0) / /"
'a'.repeat(3) // 'aaa'
Copy the code

Five checkstr[1] indexOf, etc.Return element position | | Boolean value

1) Get the stringSpecifies the value of the location charAt()andcharCodeAt()

Both the charAt() and charCodeAt() methods can be indexed to get a value at a given location:

  • The charAt() method getsThe character that specifies the position;
  • The charCodeAt() method gets the positional characterUnicode value

CharCodeAt () returns an integer between 0 and 65535, or NaN if there are no characters at the specified position

CharCodeAt passes numbers and strings the same way.

str.charCodeAt(0); / / 97
'0'.charCodeAt('0'); / / 48
'0'.charCodeAt(0); / / 48
Copy the code

2) charAt()str[1]The difference between

STR [index] returns undefined and charAt(index) returns an empty string when index is not in the length range of STR.

var str = 'abc';
str[1]; // 'b'
str[3]; // undefined
str.charAt(3); / /"
Copy the code

3) Retrieve the stringWhether a particular sequence is included indexOf lastIndexOf includes startsWith endsWith

Note: All 5 methods are case-sensitive

3.1) indexOf lastIndexOf The position of the return value
  • IndexOf [to find in positive order]: Looks for a character,With the returnThe position that was first matchedOtherwise, -1 is returned
  • LastIndexOf [reverse lookup]: Searches for a character, if any, returnsThe location of the last matchOtherwise, -1 is returned
var str = 'abcabc';
str.indexOf('a'); / / 0
str.indexOf('a'.3); / / 3
str.indexOf('a'.4); // -1

var str = 'abcabc';
str.indexOf('bc'); / / 1
Copy the code
3.2) includes startsWith endsWith Return to Boerse
  • includes(): This method is used to judge stringsWhether to contain the specified substring
  • startsWith(): This method is used to detect stringsWhether to start with the specified substring
  • endsWith: This method is used to determine the current stringEnds in the specified substring
var str = 'abcabc';
str.includes('abcd');
str.includes('ab');
str.includes('ab'.5);
Copy the code

var str = 'abcabc';

str.startsWith('b'); // false
str.startsWith('abc'); // true

str.endsWith('bc'); // true
Copy the code

4) Get the STR itselfvalueOf()toString()

Both the valueOf() and toString() methods return the valueOf the string itself

var str = 'abc';
str.toString(); // 'abc'
str.valueOf(); // 'abc'

Copy the code

5) STR pattern matching replace() match() search()

The replace replaceAll match search method can be used to match or replace characters

var str = 'abcbc';
str.replace('bc'.1); // 'a1bc'
var str = 'abcbc';
str.replaceAll('bc'.1);//'a11'
Copy the code
var str = 'aBCabc';
var str1 = str.replace(/ab/gi.1);
console.log(str,str1);
// 'aBCabc' '1C1c'
Copy the code
var str = 'aBCabc';
str.match('ab');
// ['ab', index: 3, input: 'aBCabc', groups: undefined]
var str = 'aBCabc';
str.match('d'); // null
Copy the code
var str = "abcdef";
str.search(/bcd/); / / 1
var str = "abcdef";
str.search('c'); / / 2
Copy the code

reference

  • JS top 20 common string methods and their usage
  • Js 28 common string methods and use tips

conclusion

  • IndexOf [to find in positive order] LastIndexOf [reverse lookup]
  • substr slice substringCan be used to intercept strings,They don't change the raw data The second parameter is optional The first two first arguments can both be passed negative numbers
  • slice Positive sequence rules Do not change the raw data Both parameters can be negative