Java lastindexof and indexof Methods

Source: Internet
Author: User

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

Str2 = "ob ";
S = 17;

Indexof Method

Returns the first occurrence of a substring in a string object.

Strobj. indexof (substring [, startindex])

Parameters
Strobj

Required. String object or text.

Substring

Required. The substring to be searched in the string object.

Starindex

Optional. This integer indicates the index in the string object. If omitted, search from the start of the string.

Description
The indexof 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 left to right. Otherwise, this method is the same as lastindexof.

Example
The following example illustrates how to use the indexof method.

Function indexdemo (str2 ){
VaR str1 = "babebibobubabebibobu"
VaR S = str1.indexof (str2 );
Return (s );
}

========================================================== ======================================

Add QQ: 284651035 (this blogger) for help)

Weibo: http://weibo.com/cnbailu (Bai Lu)

Http://weibo.com/u/2717396075 (featured jokes)

Mr Chen Weibo: http://weibo.com/redarmychen Java direction

Miss Li Weibo: http://weibo.com/csdnliwei PHP direction

Mobile (mobile) Software Development Exchange Group: 57636364(Please note that the application is on the csdn blog. Remember to change your business card after entering the group.) Thank you!

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.