This is the 10th day of my participation in the August More Text Challenge

String method

For more examples of methods, see JavaScript String objects.

methods describe
charAt() Returns a character at the specified index position
charCodeAt() Returns the Unicode value of the character at the specified index position
concat() Concatenate two or more strings and return the concatenated string
fromCharCode() Converts Unicode to a string
indexOf() Returns the position in a string to retrieve the first occurrence of the specified character
lastIndexOf() Returns the position in a string that retrieves the last occurrence of the specified character
localeCompare() Compare two strings in a locally specific order
match() Finds a match for one or more regular expressions
replace() Replaces the substring that matches the regular expression
search() Retrieves the value that matches the regular expression
slice() Extracts a fragment of a string and returns the extracted portion in a new string
split() Splits a string into substring arrays
substr() Extracts a specified number of characters from the initial index number in the string
substring() Extracts the character between two specified index numbers in a string
toLocaleLowerCase() The string is converted to lowercase based on the host’s locale, and only a few languages (such as Turkish) have locally-specific case mapping
toLocaleUpperCase() The string is converted to uppercase depending on the host’s locale, and only a few languages (such as Turkish) have locally-specific case mapping
toLowerCase() Converts a string to lowercase
toString() Returns a string object value
toUpperCase() Converts the string to uppercase
trim() Removes whitespace at the beginning and end of a string
valueOf() Returns the original value of a string object
Eleven: Slice () method
Definitions and Usage

The slice() method returns selected elements from an existing array.

The instance

Example 1

In this case, we will create a new array and display the elements selected from it:

<script type="text/javascript">

var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

document.write(arr + "<br />")
document.write(arr.slice(1) + "<br />")
document.write(arr)

</script>
Copy the code

Output:

George,John,Thomas
John,Thomas
George,John,Thomas
Copy the code

Example 2

In this case, we will create a new array and display the elements selected from it:

<script type="text/javascript"> var arr = new Array(6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "James" arr[4] = "Adrew" arr[5] = "Martin" document.write(arr + "<br />") document.write(arr. Slice (2,4) + "<br />") document.write(arr) </script>Copy the code

Output:

George,John,Thomas,James,Adrew,Martin
Thomas,James
George,John,Thomas,James,Adrew,Martin
Copy the code
Twelve: split() method
Definitions and Usage

The split() method splits a string into an array of strings.

The instance

Example 1

In this case, we will split the string in a different way:

<script type="text/javascript">

var str="How are you doing today?"

document.write(str.split(" ") + "<br />")
document.write(str.split("") + "<br />")
document.write(str.split(" ",3))

</script>
Copy the code

Output:

How,are,you,doing,today?
H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
How,are,you
Copy the code

Example 2

In this case, we will split a string with a more complex structure:

"2:3:4:5". The split (" : ") / / returns [" 2 ", "3", "4", "5"] "| a | b | c". The split (" | ") / / returns [" ", "a", "b", "c"]Copy the code

Example 3

Use the following code to break sentences into words:

Separator: var words = sentence.split(") or use the regular expression as separator: var words = sentence.split(/\s+/)Copy the code

Example 4

If you want to split words into letters, or strings into characters, use the following code:

"Hello". The split (" ") can be returned / / [" h ", "e", "l", "l", "o"] if only need to return part of the characters, please use the howmany parameters: "hello". The split (" ", 3) can be returned / / [" h ", "e", "l"]Copy the code
Thirteen: substr() method
Definitions and Usage

The substr() method extracts a specified number of characters from the start subscript in the string.

The instance

Example 1

In this case, we’ll use substr() to extract some characters from the string:

<script type="text/javascript">

var str="Hello world!"
document.write(str.substr(3))

</script>
Copy the code

Output:

lo world!
Copy the code

Example 2

In this case, we’ll use substr() to extract some characters from the string:

<script type="text/javascript"> var str="Hello world!" Document. The write (STR) substr (3, 7)) < / script >Copy the code

Output:

lo worl
Copy the code
14: the substring ()
Definitions and Usage

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

The instance

Example 1

In this case, we’ll use substring() to extract some characters from the string:

<script type="text/javascript">

var str="Hello world!"
document.write(`str.substring(3)`)

</script>
Copy the code

Output:

lo world!
Copy the code

Example 2

In this case, we’ll use substring() to extract some characters from the string:

<script type="text/javascript"> var str="Hello world!" Document. The write (` STR. The substring (3, 7) `) < / script >Copy the code

Output:

lo w
Copy the code
15: toLocaleLowerCase ()
Definitions and Usage

The toLocaleLowerCase() method is used to convert a string to lowercase.

The instance

In this case, “Hello World!” Will be displayed in lower case:

<script type="text/javascript">

var str="Hello World!"
document.write(str.toLocaleLowerCase())

</script>
Copy the code
16: toLocaleUpperCase ()
Definitions and Usage

The toLocaleUpperCase() method is used to convert a string to uppercase.

The instance

In this case, “Hello World!” Will be displayed in capital letters:

<script type="text/javascript">

var str="Hello World!"
document.write(str.toLocaleUpperCase())

</script>
Copy the code
17: toLowerCase ()
Definitions and Usage

The toLowerCase() method is used to convert a string toLowerCase.

The instance

In this case, “Hello World!” Will be displayed in lower case:

<script type="text/javascript">

var str="Hello World!"
document.write(str.toLowerCase())

</script>
Copy the code
18: the toString ()
Definitions and Usage

The toString() method converts a Number object to a string and returns the result.

The instance

In this case, we will convert a number to a string:

<script type="text/javascript">

var number = new Number(1337);
document.write (number.toString())

</script>
Copy the code

Output:

1337
Copy the code
19: toUpperCase ()
Definitions and Usage

The toUpperCase() method is used to convert a string toUpperCase.

The instance

In this case, “Hello World!” Will be displayed in capital letters:

<script type="text/javascript">

var str="Hello World!"
document.write(str.toUpperCase())

</script>

Copy the code
20: trim ()
Definitions and Usage

The trim() method removes leading and trailing whitespace from a string. Whitespace includes Spaces, tabs, newlines, and other whitespace characters.

The trim() method does not alter the original string.

The trim() method does not work with null, undefined, or Number types.

The instance

function myTrim(x) {
  return x.replace(/^\s+|\s+$/gm,'');
}
 
function myFunction() {
  var str = myTrim("        Runoob        ");
  alert(str);
}
Copy the code

The output

Runoob 
Copy the code
21: valueOf()
Definitions and Usage

The valueOf the valueOf() method is mandatory String original value.

Note: The valueOf() method is usually called automatically in the background by JavaScript, not explicitly in code.

The instance

Mandatory String The original value of the object:

<script> var str="Hello world!" ; document.write(str.valueOf()); </script>Copy the code

The output of the above example is as follows:

Hello world!
Copy the code