Java indexof ()

Source: Internet
Author: User
Document directory
  • Indexof
  • Indexof

Int indexof (int ch, int fromindex) function: it is the first position of the character ch after the string fromindex bit.-1 is not found.

Indexof
public int indexOf(int ch)
Returns the index of the first occurrence of the specified character in this string. If StringThe value in the Character Sequence represented by the object is chReturns the index that appears for the first time (expressed in Unicode code units ). For values between 0 and 0 xFFFF (including 0 and 0 xFFFF) chValue. The returned value is

 this.charAt(k) == ch 

Minimum value of trueKValue. For otherchValue. The returned value is

 this.codePointAt(k) == ch 

True minimumKValue. In either case, if this string does not contain such a character-1.

 

Parameters:
ch-One character (UNICODE code point ).
Return Value:
Index of the character for the first time in the Character Sequence represented by this object; if this character is not displayed, the return value is -1.
Indexof
public int indexOf(int ch,                   int fromIndex)
Returns the index that appears at the specified character for the first time in this string and starts searching from the specified index.

HereStringIn the Character Sequence represented by an objectchThe character index of is not lessfromIndexReturns the index with this value for the first time. For values between 0 and 0 xFFFF (including 0 and 0 xFFFF)chValue. The returned value is

 (this.charAt(k) == ch) && (k >= fromIndex) 

Minimum value of trueKValue. For otherchValue. The returned value is

 (this.codePointAt(k) == ch) && (k >= fromIndex) 

Minimum value of trueKValue. In either case, iffromIndexOtherwise, the system returns-1.

fromIndexThere is no limit on the value. If it is negative, it returns the same effect as 0: the entire string is searched. If it is greater than the length of this string, it will have the same effect as it is equal to the length of this string:-1.

All indexes are incharValue (UNICODE code unit ).

 

Parameters:
ch-One character (UNICODE code point ).
fromIndex-Start to search for the index.
Return Value:
The first occurrence of a character sequence in this object is greater than or equal fromIndexIf this character is not displayed, the return value is -1.

Eg: String STR = "a2dfcfar1bzvb ";
System. Out. println (Str. indexof (97,2 ));

Let's look at this example. The output is 6.
If the ASCII value of A is 97, search for a from D and find the exact position of the string where output A is located. If no value is found, the output is-1! (The first position in Java starts from 0)

String. indexof function usage Summary
1. The indexof parameter is string, startindex: number;
   The return value of indexof is int,
2. Function indexof contains the following formats:
1). strng. indexof (substring) // search for the substring in string. The default value starts from 0;
2). String. indexof (substring, int m) // search for the substring in string. The default value starts from the position M;
Public class hehe
{
 
 
 Int I;
 Int X;
 String IP = NULL;
 String input1 = NULL;
 String input2 = NULL;
 Public void main (string ARGs []) {
 IP = "126.168.1.1 ";
 I = IP. indexof ('.');
 X = IP. indexof ('.', I + 1 );
 Input1 = IP. substring (0, I );
 Input2 = IP. substring (I + 1, x );
 
 System. Out. println ("Input1 is "+ input1 );
 System. Out. println ("The input2 is" + input2 );
                                 }
}
The result is
TheInput1 is 126
The input2 is 168

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.