Common methods of strings.html

<script type = "text / javascript">
// Common methods for strings (without changing the original string) <ES5
var str = 'hello world'

//1.charAt () Find the contents of the string according to the index and return
/ / Syntax: string.charAt (index to be searched)
// Return value: string corresponding to the index (there is no empty string)
var res = str.charAt (4)
console.log (str) // hello world
console.log (res) // o


//2.charCodeAt ()
// Syntax: string.charCodeAt (index to be searched)
// Return value: the encoding of the character corresponding to the index position (decimal)
var res = str. charCodeAt (0)
console.log (str) // hello world
console.log (res) // 104


//3.substring () interception string
// Syntax: string.substring (start position index, end position index)
Return value before / after package : the character at the interception position (not before and after package)
var res = str.substring (0,4)
console.log (str) // hello world
console.log (res) // hell


//4.substr () intercept string
// Syntax: string.aubstr (index of starting position, how many)
// Return value: character at position
var res = str.substr (2,5)
console.log (str) // hello world
console.log ( res) //


llo w //5.concat () concatenated string
// syntax: string.concat (string that needs to be concatenated)
// return value: concatenated string
var res = str.concat ('you Good world ',' linux access') // = str + 'hello world'
console.log (str) // hello world
console.log (res) // hello world hello world linux access


//6.slice () The string interception is the same as the substring () method
// Syntax: string. Slice (start position index, end position index) before and after the package
// return value: the intercepted string
var res = str.slice (0,4 )
console.log (str) // hello world
console.log (res) // hell


//7.toUpperCase () string to uppercase
// Syntax: string.toUpperCase ()
// Return value: string uppercase content
var res = str.toUpperCase ()
console.log (str) // hello world
console.log (res) // HELLO WORLD

var str3 = '123456'
var res = str3.toUpperCase ()
console.log (str3) // 123456
console.log (res) / / 123456

var str2 = 'HELLO WORLD'
//8.toLowerCase () string to lowercase
// Syntax: string.toLowerCase ()
// Return value: string lowercase content
var res = str2.toLowerCase ()
console.log (str2) // HELLO WORLD
console.log (res) // hello world


var str4 = '1-2-3-4-5-6'
//9.split () string cutting, according to the conditions we give, Divide the string into sections, and then return in an array
// Syntax: string.split (what to use for cutting)
// Return value: return an array
var res = str4.split ('-') // pass empty When string, it is cut by one
console.log (str4) // 1-2-3-4-5-6
console.log (res) // (6) ["1", "2" , "3", "4", "5", "6"]


var str5 = 'Hello, world, hello, world, hello, world'
//10.replace () replaces the contents of the string (for text query)
// Syntax: string.replace (replace , replace What)
var res = str5.replace ('hello', 'hello')
console.log (res) // hello, world, hello, world, hello, world
console.log (str5) // hello , World, hello, world, hello, world (only one can be replaced at a time)


// common string method ES5

//1.indexOf () Find the index position of a certain character
// first way

// syntax: String. IndeOf (character fragment)
// Return value:
// If the corresponding character fragment is found, then the index of the starting position of the character fragment is returned
// If the corresponding character fragment is not found. Then return -1
var str6 = 'hello world'
var res = str6.indexOf ('lo')
console.log (str6) // hello world
console.log (res) // 3

// Second way
// syntax : String. IndeOf (character fragment (can only be one character, otherwise return -1), from which index position to start searching)
var res = str6.indexOf ('l', 5)
console.log (str6) // hello world
console.log(res) //9

</script>

Guess you like

Origin www.cnblogs.com/d534/p/12711989.html