Using JavaScript to implement an intercept string containing Chinese processing functions _javascript techniques

Source: Internet
Author: User

1.substring method
Definitions and usage
The substring method is used to extract characters from a string mediation between two specified subscripts.
Grammar
Stringobject.substring (Start,stop)
Parameter description
Start Required. A non-negative integer that stipulates the position of the first character of the substring to be extracted in the stringobject.
Stop is optional. A non-negative integer that is 1 more than the last character of the substring to extract in the Stringobject. If this argument is omitted, the returned substring continues to the end of the string.
return value
A new string that contains a substring of stringobject whose contents are all characters from the start to the stop-1, whose length is stop minus start.
Description
The substring returned by the substring method includes the character at start, but does not include the character at the end.
If start is equal to end, then the method returns an empty string (that is, a length of 0).
If start is larger than end, the method swaps the two parameters before extracting the substring.
If start or end is a negative number, it is replaced with 0.

2.substr Method
Definitions and usage
The Substr method returns a substring of the specified length starting at the specified position.
Grammar
Stringobject.substr (start [, length])
Parameter description
Start Required. The starting position of the desired substring. The index of the first character in the string is 0.
Length is optional. The number of characters that should be included in the returned substring.
Description
If length is 0 or negative, an empty string is returned.
If this argument is not specified, the substring continues to the end of the stringobject.
For Example:

Copy Code code as follows:

var str = "0123456789";
Alert (str.substring (0));------------"0123456789"
Alert (str.substring (5));------------"56789"
alert (str.substring);-----------""
alert (str.substring);-----------""
Alert (str.substring ( -5));-----------"0123456789"
Alert (str.substring ( -10));----------"0123456789"
Alert (str.substring ( -12));----------"0123456789"
Alert (str.substring (0,5));----------"01234"
Alert (str.substring (0,10));---------"0123456789"
Alert (str.substring (0,12));---------"0123456789"
Alert (str.substring (2,0));----------"01"
Alert (str.substring (2,2));----------""
Alert (str.substring (2,5));----------"234"
Alert (str.substring (2,12));---------"23456789"
Alert (str.substring (2,-2));---------"01"
Alert (str.substring ( -1,5));---------"01234"
Alert (str.substring ( -1,-5));--------""
Alert (STR.SUBSTR (0));---------------"0123456789"
Alert (STR.SUBSTR (5));---------------"56789"
alert (STR.SUBSTR);--------------""
alert (STR.SUBSTR);--------------""
Alert (STR.SUBSTR ( -5));--------------"0123456789"
Alert (STR.SUBSTR ( -10));-------------"0123456789"
Alert (STR.SUBSTR ( -12));-------------"0123456789"
Alert (STR.SUBSTR (0,5));-------------"01234"
Alert (STR.SUBSTR (0,10));------------"0123456789"
Alert (STR.SUBSTR (0,12));------------"0123456789"
Alert (STR.SUBSTR (2,0));-------------""
Alert (STR.SUBSTR (2,2));-------------"23"
Alert (STR.SUBSTR (2,5));-------------"23456"
Alert (STR.SUBSTR (2,12));------------"23456789"
Alert (STR.SUBSTR (2,-2));------------""
Alert (STR.SUBSTR ( -1,5));------------"01234"
Alert (STR.SUBSTR ( -1,-5));-----------""

3, IndexOf
Returns the character position for the first occurrence of a substring within a string object.
Strobj.indexof (substring[, StartIndex])
Parameters
Strobj
Required option. A String object or text.
SubString
Required option. The substring to find in the string object.
Starindex
Options available. An integer value that indicates the index in which to start the lookup within a String object. If omitted, it is looked up from the beginning of the string.
Description
The IndexOf method returns an integer value that indicates the starting position of the substring of the string object. If no substring is found, returns-1.
If the startindex is a negative number, the startindex is treated as zero. If it is larger than the maximum character position index, it is considered to be the largest possible index.
Performs a lookup from left to right. Otherwise, the method is the same as LastIndexOf.
Example
The following example illustrates the use of the IndexOf method.
Copy Code code as follows:

function Indexdemo (str2) {
var str1 = "Babebibobubabebibobu"
var s = str1.indexof (STR2);
return (s);
}


Copy Code code as follows:

/* 2007-11-28 Xujian * *
Intercept string contains Chinese processing
(string, length, increase ...)
function subString (str, Len, Hasdot)
{
var newlength = 0;
var newstr = "";
var chineseregex =/[^\x00-\xff]/g;
var Singlechar = "";
var strlength = str.replace (Chineseregex, "* *"). Length;
for (var i = 0;i < strlength;i++)
{
Singlechar = Str.charat (i). toString ();
if (Singlechar.match (Chineseregex)!= null)
{
Newlength + 2;
}
Else
{
newlength++;
}
if (Newlength > Len)
{
Break
}
Newstr + = Singlechar;
}

if (Hasdot && strlength > Len)
{
Newstr + = "...";
}
return newstr;
}

<textarea id="runcode11027"><script>/* 2007-11-28 Xujian *//intercept string contains Chinese processing//(string, length, increase ...) function subString (str, len, Hasdot) {var newlength = 0; var newstr = ""; var chineseregex =/[^\x00-\xff]/g; var Singlechar = ""; var strlength = str.replace (Chineseregex, "* *"). Length; for (var i = 0;i < strlength;i++) {Singlechar = Str.charat (i). toString (); if (Singlechar.match (Chineseregex)!= null) {newlength = 2; else {newlength++; } if (Newlength > Len) {break; } newstr + = Singlechar; } if (Hasdot && strlength > Len) {newstr + = "..."; return newstr; Alert (subString ("string interception test Www.jb51.net is a website for collecting fine learning materials", 10)); </script></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.