Two usages of Java indexOf ()

Source: Internet
Author: User

int indexOf (int ch,int fromIndex) function: is the first position that the character Ch appears after the string FromIndex bit. No Rebates found-1

Eg:string str= "A2DFCFAR1BZVB";

System.out.println (Str.indexof (97,2));

See this example, output: 6

A's ASCII is 97, starting from D to find a the exact position of the string where the output A is located, not found on the output-1! (the first position in Java starts from 0)

Summary of String.IndexOf function usage

1. The parameters of the indexof are string, startindex:number;

The return value of indexof is int,

2. Function IndexOf contains the following formats:

1). Strng.indexof (substring)//Search for substring in string, starting from 0 bits by default;

2). String.IndexOf (substring, int m)//search for the substring in String, starting from the first m bit by default;

Package Com.xsit;public class Testindexof {static int i;static int x;static String IP = null;static string input1 = null;< c0/>static String input2 = null;    public static 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 ("The  input1 is" + input1); System.out.println ("The Input2 is" + Input2);}


Two usages of Java indexOf ()

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.