Common methods for JavaScript strings _javascript Tips

Source: Internet
Author: User
Tags string methods stringbuffer

Get class:

1) Dynamic Method:

CharAt: Gets the character at the specified position of the string. (parameter: one that specifies the position of the character to get)

1, do not accept negative numbers, if negative, you will return an empty string.

2, if the parameter is not given, the default is to get the character on the No. 0 position.

3, only one parameter is received.

charCodeAt: Gets the Unicode encoding (parameter: one, specifying the character position to get the character encoding) for the character at the specified position in the string

1, any character has a unique character encoding.

2, only one parameter is received.

Common:

Number: 48 ~ 57

Underline: 95

Space: 32

tab characters: 9

Lowercase letters: 97 ~ 122

Capital Letter: 65 ~ 90

2 static method:

fromCharCode: Returns the corresponding character according to the specified character encoding. (parameter: any number)

1, you can receive multiple parameters.

2, the wording is fixed (static method): String.fromCharCode (); Character encoding valid range: 0 ~ 65535 string is an object of a string

var str = ' I am a string ';
Alert (Str.charat ()); "If the length of the only, but find Str.charat () can not find is also an empty string", ~str.length-is the legal range.
alert (Str.charat ());///' I ' default does not write is, find first character
alert (Str.charat ());//' word '
alert ('. charAt ());//
Alert ('. CharAt (,));
alert (Str.charcodeat ());//Unicode Encoding
alert ('. charCodeAt ()); 

Find class:

IndexOf: Finds where the specified substring appears for the first time in the string. (The first argument specifies the substring to find; the second parameter specifies where to start the lookup.) )

1, look back in the past and start with the No. 0 position by default.

2, if found, return to the first found location, if not found, return-1.

3, if the second argument is negative, default is treated as 0来

LastIndexOf: Finds the last occurrence of the specified substring in the string. (The first argument specifies the substring to find; the second parameter specifies where to start the lookup.) )

1, from the back of the lookup, the default from the position of the first length-1.

2, if found, return to the first found location, if not found, return-1.

var str = ' www.baidu.com/';
Alert (str.indexof (' Bai ')); Find one from left to right and return no more to the right.
alert (Str.indexof (' m ',))//From the beginning to the right to find
alert (str.indexof (' X '))//-does not exist The result is-said not found

Intercept class:

SUBSTRING: Extracts a segment of a substring of the specified range. (the first parameter, specifying the start position to extract, and the second parameter specifying the end position to extract.) )

1, the extraction range includes the start position, but does not include the end position.

2, you can omit the second argument, which indicates that you extracted from the start position to the end of the string

3, before the extraction will be compared to the size of two parameters, and then from small to large order to adjust the parameter position, and then extract.

4, all the illegal parameters will be automatically converted to 0.

5, if the argument is not given, the default returns the entire string directly.

Slice: Extracts a segment of a substring of the specified range. (the first parameter, specifying the start position to extract, and the second parameter specifying the end position to extract.) )

1, the extraction range includes the start position, but does not include the end position.

2, you can omit the second argument, which indicates that you extracted from the start position to the end of the string

3, does not compare the position of two parameters, but also does not adjust position

4, the argument can be positive or negative, and all other illegal arguments will be converted to 0.

5, a negative number represents the position of the forward digit character from the back of the string, and the position of the most characters is-1.

var str = ' I am a string ';
Alert (str.substring ()); ' I am the string '
alert (str.substring (-,));//' I '
alert (str.substring ());//String
alert (str.substring (,)); I am ' the same as str.substring (,). Can detect two number, large back still, small forward still. Negative numbers as a deal.
Alert (Str.slice (,));//blank not found, no swap position

Comparison class:

Alert (' I ' > ' you '); True string Comparisons Compare the Unicode value size of the first character, and the back is not compared.

Other classes:

alert (str.length); Get string length

Split ()///Cut the string into an array

Parameter: A specified delimiter is used to split a string.

1, if you do not specify a delimiter, will not be split, directly into the array.

2, on the basis of the separator, the values of the left and right sides of the separator are stored in the array.

3, the separator itself is not stored in an array.

4, a delimiter can only be a substring that exists in a string.

5, in Split view, two characters must rely on an empty string connection.

6, when an empty string is used to split an empty string, an empty array is obtained.

var str = ';
Alert (typeof str); String
alert (typeof Str.split ()),//object
alert (str.split (). length);//['
alert (Str.split (')); /[', ']
alert (str.split (' a '));//[']
alert (Str.split ("));//[', '"
Alert (Str.split ("). length); /[', ' ', ', ', '] alert (Str.split ('));//[', ', ', ', ', ', ', ', ', ', ', ', ', ', ' and '/'
by ' five ' plus four characters
alert (Str.split ("));//[', ' ']
alert (str.split ('));//[', ']
alert ('. Split (') '. length);//['
alert ('. Split ('). length); //[]

Trim (): Removes all whitespace from the end of the string. (The space in the middle of the string is preserved).

HTML5 new method, low version browser does not support.

toUpperCase (): Converts all strings to uppercase. (No parameters)

toLowerCase (): All the strings are converted to lowercase. (No parameters)

Finally, all the string methods do not modify the string itself.

JavaScript string Connection Class

When we write the front end of JS, there will often be a lot of strings through the "+" stitching up, and then mount a DOM element. However, on the use of "+" to splice strings, in the browser to parse the effect, I do not do this in contrast, the online comparison of a lot of these. Many cows say that the concatenation of strings by using the array join method in JS is very good. To do this in the project to write a JS class, used to unify the processing string concatenation.

Code

A custom string connection class that is used to stitch strings, to elevate performance function StringBuffer ()
{ 
this._strs = new Array () than "+" 
StringBuffer.prototype.append = function (str)
{ 
this._strs.push (str); 
StringBuffer.prototype.arrayToString = function () 
{return 
this._strs.join (""); 

And when we use this class, we can go directly to the following methods:

var strbuff=new stringbuffer (); Strbuff.append ("Hello,"); Strbuff.append ("Welcome to javascript!"); Alert (strbuff.arraytostring ());
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.