SoFunction
Updated on 2025-03-01

Use javascript to implement functions that intercept strings containing Chinese processing

method
Definition and usage
The substring method is used to extract characters in a string between two specified subscripts.
grammar
(start,stop)
Parameter Description
start Required. A non-negative integer that specifies the position of the first character of the substring to be extracted in stringObject.
stop optional. A non-negative integer has 1 more position in stringObject than the last character of the substring to be extracted. If this parameter is omitted, the returned substring will continue to the end of the string.
Return value
A new string whose value contains a substring of stringObject whose content is all characters from start to stop-1, with a length of stop minus start.
illustrate
The substring method returns a substring including the characters at start, but not the characters at end.
If start and end are equal, then the method returns an empty string (that is, a string of length 0).
If start is larger than end, the method will swap these two parameters before extracting the substring.
If start or end is negative, then it will be replaced with 0.

method
Definition and usage
The substr method returns a substring of a specified length starting from the specified position.
grammar
(start [, length ])
Parameter Description
start Required. The starting position of the required substring. The index of the first character in the string is 0.
length optional. The number of characters that should be included in the returned substring.
illustrate
If length is 0 or negative, an empty string will be returned.
If this parameter is not specified, the substring will continue to the end of the stringObject.
For example:

Copy the codeThe code is as follows:

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

3、 indexOf 
Returns the character position of the first substring in the String object.
(subString[, startIndex])
parameter
strObj
Required option. String object or text.
subString
Required option. The substring to look for in the String object.
starIndex
Optional. This integer value indicates the index that begins searching within the String object. If omitted, look up from the beginning of the string.
illustrate
The indexOf method returns an integer value indicating the start position of the substring inside the String object. If no substring is found, return -1.
If startindex is a negative number, startindex is treated as zero. If it is larger than the largest character position index, it is regarded as the largest possible index.
Perform a lookup from left to right. Otherwise, the method is the same as lastIndexOf.
Example
The following example illustrates the usage of the indexOf method.
Copy the codeThe code is as follows:

function IndexDemo(str2){
var str1 = "BABEBIBOBUBABEBIBOBU"
var s = (str2);
return(s);
}


Copy the codeThe code is as follows:

/* 2007-11-28 XuJian */ 
//Intercept the string and include Chinese processing
//(string, length, increase...)
function subString(str, len, hasDot) 

    var newLength = 0; 
    var newStr = ""; 
    var chineseRegex = /[^\x00-\xff]/g; 
    var singleChar = ""; 
    var strLength = (chineseRegex,"**").length; 
    for(var i = 0;i < strLength;i++) 
    { 
        singleChar = (i).toString(); 
        if((chineseRegex) != null) 
        { 
            newLength += 2; 
        }     
        else 
        { 
            newLength++; 
        } 
        if(newLength > len) 
        { 
            break; 
        } 
        newStr += singleChar; 
    } 

    if(hasDot && strLength > len) 
    { 
        newStr += "..."; 
    } 
    return newStr; 
}


[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]