Seven string

All string methods are JS function calls in a string provided, returns a new result, no effect on the original string

String index is started from 0, it represents the first character in the string

1, length attribute obtaining length of the string (number of single character) 

var  str1 = "hello world"

was str2 = 'ABC'

console.log (str1.length) // 11 (the space will be resolved into a binary number 0 and a computer composition)

2, charAt () Gets a character string specified position (the charAt method is a character string, with the return value)

var res1 = str1.charAt (0)    

console.log(res1)               //     h

3, concat () connected to two strings, the return value is the new string

var res2 = str1.concat(str2)

console.log(res2)               //      hello worldabc

4, indexOf () returns the position of the first occurrence of a character in a string

was RES3 = str1.indexOf ( 'o')

console.log(res3)               //       4

5, lastIndexOf () returns the position of a character in the string of the last occurrence of

was RES4 = str1.indexOf ( 'o')

console.log(res4)               //       7

6, replace () specified replacement string, returns a new string    (only replace the first)

var res5 = str1.replace ( 'o', 'dream Whispering Color')

console.log(res5)

7, split () to convert the string into an array in a predetermined form

1) parameter is empty, the entire string as a single element of the array into the

was str3 = 'sfcq'

var arr1 = str3.split()      //  ["sfcq"]

2) the parameter is an empty string, the original string to the empty string cutting

var arr2 = str3.split('')     //   ['s','f','c','q']

3) parameter to specify the character

was arr3 = str.split ( 'c')

console.log(arr3)          //     ['sf','q']

Join method distinction split method and array of strings

The Join // ()
// // the Join is to use a connector to each element of the array into a string connection, the parameter is empty, the default connection comma
var arr = [ 'a', 'b', 'C']
var str1 arr.join = () // 'A, B, C'
var str2 = arr.join ( '') // 'ABC'
var Str3 = arr.join ( '') // 'ab & C '
var str4 = arr.join (' ~ ') //' A ~ B ~ C '

// Split ()
// // Split method is one by one into the string array with a specified character after cutting, the cutting breaks empty, by default the entire string into the array as a whole
var newStr = 'sfcq'
var newArr1 newStr.split = () // [ "sfcq"]
var = newArr2 newStr.split ( '') // [ 'S', 'F', 'C', 'Q']
var = newArr3 newStr.split ( 'C') // [ 'SF', 'Q']

Guess you like

Origin www.cnblogs.com/liankong/p/10991775.html