JS Action String

Posted by bj_ on Sun, 09 Jun 2019 18:09:03 +0200

1: length

Get String Length

var str = "hello world";
alert(str.length);   //11

2: Index

Gets the character at the specified position of the string through an index, but cannot change the value corresponding to that index

var str = "hello world"
alert(str[0]);   //h
str[0] = "H";//str will not be affected, but no errors will be reported

3: toUpperCase

Converts all strings to uppercase and returns them, leaving the original string unchanged

var str = "hello world";
alert(str.toUpperCase());

4: toLowerCase

Converts all strings to lowercase and returns them, leaving the original string unchanged

var str = "hello world";
alert(str.toLowerCase());

5: indexOf

Searches for the location of the specified string, accepting two parameters, the first parameter is the element to find, the second parameter is the location to find; if found, returns the location of the corresponding element, otherwise returns -1

var str='hello world'
alert(str.indexOf('world'))  //6
alert(str.indexOf('World')  //-1
alert(str.indexOf('0'))  //4
alert(str.indexOf('0',6))  //Search from 6th place, return 7

6: lastIndexOf

Like indexOf, except it starts at the end of the string, finds the return coordinate, and finds no return-1.

7: concat

Stitches one or more strings together and returns the stitched new string without changing the original string

var str1 = "hello ";
var res = str1.concat(" world","!");
alert(res);//Return to hello world!
alert(str1);//Return to hello

8: slice

Returns a substring of the character to be manipulated, the original string is unchanged, accepts two parameters, the starting and ending position of the string, returns a string that does not contain the character of the ending position, the first parameter is less than the second parameter, otherwise returns ", if there is only one parameter, returns all strings from the beginning to the end of the string, if the parameter passed is negative, it willThe incoming negative value is added to the length of the character wound

var str = "hello world!";
alert(str.slice(3,7));//Return to lo w
alert(str.slice(3));//Return to lo world!
alert(str.slice(9,5));//Return to ""
alert(str.slice(-7,-3));//Negative numbers add to length, that is, str.slice(5,9) returns wor
alert(str.slice(5,9));//Return to wor

9: substring

When the parameter passed in is positive, substring and slice function basically the same, the only difference is that when the first parameter is greater than the second parameter, the second parameter will be used as the starting position of intercept, the first parameter will be used as the ending position of intercept, and the intercepted string will not contain the value corresponding to the first parameter position; when the parameter passed in is negative, the other side will changeThe method converts all negative values to zero;

var str = "hello world!";
alert(str.substring(3,7));//Return to lo w
alert(str.substring(3));//Return to lo world!
alert(str.substring(9,5));//Returns wor d, str.substring(5,9), without Item 9
alert(str.substring(-7,-3));//Negative numbers are added to length, that is, str.substring(0,0) returns ""
alert(str.substring(-7,3));//Negative numbers add up to length, that is, str.substring(0,3) returns hel

10: substr

Returns a string of the specified length starting at the specified position, unchanged from the original string; truncates to the end of the string by default if the second parameter is passed; when the passed parameter is negative, the method adds the negative first parameter to the length of the string and converts the negative second parameter to 0''

var s = 'hello world!'
alert(s.substr(0, 5));//From Index0Start, intercept5String, return hello
alert(s.substr(7)); //From Index7Start intercepting, end, return orld!
alert(s.substr(-7,3));//Negative numbers add up to length, that is str.substr(5,3),Return wo
alert(s.substr(-7,-3));//Negative numbers add up to length, that is str.substr(5,0),Return""

11: split

Divides a string into multiple strings based on a specified delimiter and stores the result in an array; two parameters can be passed, the first being a delimiter; the first specifying the size of the returned array; and if the parameter is omitted, the entire array is returned

var s = "1,23,45";
var arr1 = s.split(",");
alert(arr1);//Return Array["1","23","45"]
var arr2 = s.split(",",2);
alert(arr2);//Return Array["1","23"]

12: trim

Remove all spaces before and after the element and return the result, leaving the original array unchanged

var str1 = "      hello world   ";
var str2 = str1.trim();
alert(str1);//Return to "hello world"
alert(str2);//Return to "hello world"

13: String

Convert any type of data to a string

var num= 19; // 19
var myStr = num.toString(); // "19"

14: replace

Replace some characters with others in the string, or a substring that matches a regular expression

stringObject.replace(regexp/substr,replacement)

The first parameter must specify the string or the RegExp object of the pattern to be replaced; if the value is a string, it will be used as the direct amount text pattern to be retrieved, not converted to the RegExp object first;
The second parameter must be a string value that specifies the function used to replace or generate the replacement text.

var str="Visit Microsoft!"
document.write(str.replace(/Microsoft/, "W3School"))   //Visit W3School!

Global replacement, whenever "Microsoft" is found, it is replaced with "W3School"

var str="Welcome to Microsoft! "
str=str + "We are proud to announce that Microsoft has "
str=str + "one of the largest Web Developers sites in the world."

document.write(str.replace(/Microsoft/g, "W3School"))   //Welcome to W3School! We are proud to announce that W3School
has one of the largest Web Developers sites in the world.

Topics: less