Soft technology web class: string methods and properties

String methods to help you deal with strings.

String methods and properties

The original value, such as "Bill Gates", can not have properties and methods (because they are not objects).

However, by JavaScript, methods and properties can also be used for the original value, the original value is considered as JavaScript object when performing methods and properties.

String length

property returns the length of the string length:

Examples

var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;

Find string in a string

indexOf () method returns the index of the specified text string in the first occurrence (position):

Examples

var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China");

JavaScript is calculated from the zero position.

0 is the first position in the string, 1 is the second, third ... 2

lastIndexOf () method returns the index of the specified text string of the last occurrence of:

Examples

var str = "The full name of China is the People's Republic of China.";
var pos = str.lastIndexOf("China");

If the text is not found,  indexOf () and  lastIndexOf () both return -1.

Examples

var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("USA");

Both methods accept a second parameter as a search start position.

Examples

var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China", 18);

lastIndexOf () methods to retrieve rearwardly (from tail to head), which means: if the second parameter is 50, 50 from the position of starting a search, until the beginning of the string.

Examples

var str = "The full name of China is the People's Republic of China.";
var pos = str.lastIndexOf("China", 50);

Retrieval string in

search () method of the search string specified value, and returns the matching positions:

Examples

var str = "The full name of China is the People's Republic of China.";
var pos = str.search("locate");

You noticed?

Both methods, the indexOf ()  and  Search () , are equal .

These two methods are not equal. The difference is that:

  • search () method can not set the start position of the second parameter.
  • indexOf () method can not be set more powerful search value (regular expressions).
var str = "The full name of China is the People's Republic of China.";
var pos = str.search("locate");

Character string extraction section

There are three ways to extract part of the string:

  • slice(startend)
  • substring(startend)
  • substr(startlength)

slice () method

slice () extracts a certain portion of the string and the return portion is extracted in a new string.

The method set two parameters: starting index (starting position), a stop index (end position).

Crop this example, the string position 7 to position 13 fragment:

Examples

var str = "Apple, Banana, Mango";
var res = str.slice(7,13);

Res is the result of:

Banana

If a parameter is negative, counting from the end of the string.

Crop this example, the string position -12 to position -6 fragment:

Examples

var str = "Apple, Banana, Mango";
var res = str.slice(-13,-7);

Res is the result of:

Banana

If the second parameter is omitted, the method will cut the remaining portion of the string, or the end of the count:

Examples

was res = str.slice (7);
was str.slice res = (-13);

substring () method

substring () is similar to  slice ().

The difference is that  substring () can not accept the negative index.

Examples

var str = "Apple, Banana, Mango";
var res = str.substring(7,13);

Res is the result of:

Banana 
If the second parameter is omitted, the  substring () the remainder of the crop string.

substr () method

substr () is similar to  slice ().

Except that the length of the predetermined portion of the extracted second parameter.

Examples

var str = "Apple, Banana, Mango";
var res = str.substr(7,6);

Res is the result of:

Banana

If the second parameter is omitted, the substr () the remainder of the crop string.

Examples

var str = "Apple, Banana, Mango";
var res = str.substr(7);

Res is the result of:

Banana, Mango 

second parameter can not be negative, as it is defined length.

Source: www.sysoft.net.cn, plus v: 15844800162 depth exchange

replace ()  method does not change the call to its string. It returns a new string.

By default, the replace ()  replaces only the first match :

Examples

Guess you like

Origin www.cnblogs.com/sysoft/p/11664859.html