String index out of range:-1

Source: Internet
Author: User

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;
}
}

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.