Two days ago, it was reported that the string was out of bounds. It was found that an error occurred while taking the position of a character. In the past, lastIndexOf was used to obtain this character and it was not found in the searched string.
Solution: verify whether the character exists before obtaining the position.
If (name. lastIndexOf ("Form ")! =-1 ){
Name = name. substring (0, name. lastIndexOf ("Form "));
}
JScript Language Reference
--------------------------------------------------------------------------------
LastIndexOf Method
Returns the position where the substring of the String object appears.
StrObj. lastIndexOf (substring [, startindex])
Parameters
StrObj
Required. String object or text.
Substring
Required. The substring to be searched in the String object.
Startindex
Optional. This integer indicates the starting index position of the String object. If omitted, the search starts from the end of the string.
Description
The lastIndexOf method returns an integer indicating the starting position of the substring in the String object. If no substring is found,-1 is returned.
If startindex is negative, startindex is treated as zero. If it is larger than the index at the largest character location, it is regarded as the largest possible index.
Search from right to left. Otherwise, the method is the same as indexOf.
The following example illustrates how to use the lastIndexOf method:
Function lastIndexDemo (str2)
{
Var str1 = "BABEBIBOBUBABEBIBOBU"
Var s = str1.lastIndexOf (str2 );
Return (s );
}
It is generally used in java classes.
From http://www.chinageren.com/jc/HTML/115914_2.html
DateBean. java
{
Private String dateStr;
Private String year;
Private String month;
Private String day;
//
Public void setDateStr (String str) // set Method of private variable dateStr
{
This. dateStr = str;
}
Public String getDateStr () // get method of private variable dateStr
{
Return dateStr;
}
Public String getYear () // obtain the year String
{
Int a = dateStr. indexOf ("-"); // calculates the number of digits of the first "-".
Year = dateStr. substring (0, a); // obtain the string before the first "-".
Return year;
}
Public String getMonth () // obtain the month String
{
Int a = dateStr. indexOf ("-"); // calculates the number of digits of the first "-".
Int B = dateStr. lastIndexOf ("-"); // calculates the number of digits of the last "-".
Month = dateStr. substring (a + 1, B); // obtain the string between two "-".
Return month;
}
Public String getday () // get the string of the day
{
Int B = datestr. lastindexof ("-"); // calculates the number of digits of the last "-".
Int Len = datestr. Length (); // evaluate the length of a string
Day = datestr. substring (B + 1, Len); // obtain the string after the last "-"
Return day;
}
}