To intercept the last character.
| The code is as follows |
Copy Code |
Str.charat (str.length–1) |
Method Two: Using the Substr method under the String object
| The code is as follows |
Copy Code |
Str.substr (str.length-1,1)
|
Method Three: Using the Split method under String object
| The code is as follows |
Copy Code |
var str = "123456″; Spstr = Str.split (""); SPSTR[SPSTR.LENGTH-1]; |
Commonly used interception using substring () or slice ()
Function: Split ()
Function: To store a string partition into an array using a specified delimiter
Example:
| The code is as follows |
Copy Code |
Str= "Jpg|bmp|gif|ico|png"; Arr=thestring.split ("|"); Arr is an array that contains the character values "JPG", "BMP", "GIF", "ico", and "PNG" |
Function: John ()
Function: Merges an array into a string using the separator of your choice
Example:
| The code is as follows |
Copy Code |
var delimitedstring=myarray.join (delimiter); var mylist=new Array ("JPG", "BMP", "GIF", "ico", "PNG"); var portablelist=mylist.join ("|"); The result is jpg|bmp|gif|ico|png. |
Function: substring ()
Function: string interception, for example, to get "Minidx" from "minidxsearchengine" is necessary to use
| The code is as follows |
Copy Code |
| SUBSTRING (0,6) |
Function: IndexOf ()
Function: Returns the subscript of the first character in a string that matches a substring
| The code is as follows |
Copy Code |
var mystring= "JavaScript"; var w=mystring.indexof ("V"); W'll be 2 var x=mystring.indexof ("S"); X would be 4 var y=mystring.indexof ("Script"); y'll also be 4 var z=mystring.indexof ("key"); Z'll be-1 |
Support for Chinese
| The code is as follows |
Copy Code |
function substr (str, len) { if (!str | |!len) {return ';} Expected count: Chinese 2 bytes, English 1 bytes var a = 0; Loop count var i = 0; Temporary string var temp = '; for (i=0;i<str.length;i++) { if (Str.charcodeat (i) >255) {//increase by 2 as expected count a+=2; } Else { a++; //If the length of the count is greater than the length of the limit, return the temporary string directly if (a > Len) {return temp;} Adds the current content to the temporary string Temp + + Str.charat (i); ///If all Single-byte characters are returned directly to the source string return str; } |
For more details please see: http://www.111cn.net/wy/js-ajax/36384.htm