JS String Operation Summary

Source: Internet
Author: User
Tags locale

"MDN" https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String

"Reference" https://www.cnblogs.com/guoyeqiang/p/8178336.html

String Conversions

1. ToString ()

var age = one; var // string "One" var true ; var foundasstring = found.tostring (); // string "true"

By default, the ToString () method returns a string representation of the numeric value returned by the 10 binary format. However, you can also export binary, octal, hexadecimal.

var num=10; num.tostring (); // "Ten"num.tostring (2); // "1010"num.tostring (8); // "Num.tostring"(10); // "Ten"num.tostring (16); // a

General values have this method, but null and undefined do not have this method, you can use the transformation function string () If you do not know if the value to be converted is null or undefined

2, String ()

If the value has the ToString () method, the ToString method is called. Returns "NULL" if it is null, and returns "undefined" if it is undefined

String (true); // "true" String (null); // "NULL" String (undefined); // undefined String (10); // "Ten"

3. + string concatenation

var num=24; var mystr= "" + num;    // " the"

Character method

1, CharAt ()

2, charCodeAt ()

3, Codepointat () ES6

The character length on a non-BMP character set can be returned correctly

4, Fromcodepoint () ES6

5, normalize () ES6

String connection

1, concat () [String.prototype.concat ()]=>MDN

Method merges one or more strings with the original string, forming a new string and returning it without affecting the original string.

  str.concat(string2, string3[, ..., stringN])

var hello = "Hello,"; Console.log (Hello.concat (/**/
 

2, + operator splicing

It is strongly recommended to use the addition operator because the addition operator performs better

var mystr1= "Hello"; var mystr2= "world!" ; var newstr=mystr1+ "" +MYSTR2;    // Hello world!

String delimited

1, Splite () [STRING.PROTOTYPE.SPLITE]=>MDN

Method splits an object into a string array using the specified delimiter string to String separate the string into substrings to determine the location of each split.

str.split([separator[, limit]])

Intercepting substrings in a string

1, Slice () [STRING.PROTOTYPE.SLICE]=>MDN

The method extracts a portion of a string and returns a new string.

str.slice(beginSlice[, endSlice])

slice() 提取的新字符串包括beginSlice不包括 endSlice

2, substr () [STRING.PROTOTYPE.SUBSTR]=>MDN

method returns a character in a string starting at the specified position to the specified number of characters.

str.substr(start[, length])


3, substring () [STRING.PROTOTYPE.SUBSTRING]=>MDN

method returns a subset of a string between the start index to the end index (not included), or a subset from the start index until the end of the string.

str.substring(indexStartindexEnd])

About the difference between slice and substring:

Stringobj.slice (start, [end])
If start is negative, it is processed as length + start, where length is the number of the array. If end is negative, it is treated as a length + end, where long is the number of the array. If End is omitted, then the slice method is copied to the end of arrayobj. If End appears before start, no elements are copied into the new array.

Strvariable.substring (start, end)
If start or end is NaN or negative, replace it with 0.
The length of the substring equals the absolute value of the difference between start and end. For example, the length of the substring returned in strvar.substring (0, 3) and strvar.substring (3, 0) is 3.

String Position method

1, IndexOf () [STRING.PROTOTYPE.INDEXOF]=>MDN

The Fromindex method returns the index of the String specified value that first appears in the calling object and begins the search in the. If the value is not found, 1 is returned.

str.indexOf(searchValue[, fromIndex])


2, LastIndexOf () [STRING.PROTOTYPE.LASTINDEXOF]=>MDN

method returns the last occurrence of the specified value in the string where the method was called, or 1 if not found. Look forward from behind the string, fromIndex starting at the beginning.

String Case Conversion

1, toLocaleUpperCase [STRING.PROTOTYPE.TOLOCALEUPPERCASE]=>MDN

Converts the input string to uppercase using a localization (locale-specific) Case mapping rule and returns the result string.

str.toUpperCase()

2, toLocaleLowerCase [STRING.PROTOTYPE.TOLOCALELOWERCASE]=>MDN

method returns the value of the call string value converted to lowercase, based on any locale-specific case mapping.

str.toLowerCase()

3, toLowerCase [STRING.PROTOTYPE.TOLOWERCASE]=>MDN

str.toLowerCase()

4, toUpperCase [STRING.PROTOTYPE.TOUPPERCASE]=>MDN

str.toUpperCase()


Matching patterns for strings

1. Match () [STRING.PROTOTYPE.MATCH]=>MDN

When a string matches a regular expression, match() The method retrieves a match.

str.match(regexp);

2. Search () [STRING.PROTOTYPE.SEARCH]=>MDN

Method performs String a search match between the regular expression and the object.

str. Search ( regexp )

String substitution

1. Replace () [STRING.PROTOTYPE.REPLACE]=>MDN

method returns a new string that replaces some or all of the matching patterns with the replacement value. A pattern can be a string or a regular expression, and the replacement value can be a string or a function to be called every time a match is used.

str.replace(regexp|substr, newSubStr|function)

String removal space

1. Trim () [STRING.PROTOTYPE.TRIM]=>MDN

Method removes white-space characters from both ends of a string. The whitespace characters in this context are all whitespace characters (space, tab, No-break space, and so on) as well as any line terminator character (such as LF,CR). trim() 方法does not affect the original string itself, it returns a new string.

str.trim()

JS String Operation Summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.