Java indexOf () method

Source: Internet
Author: User

Java indexOf () method


1. Method Introduction

(1) indexOf (int ch) returns the index at the first occurrence of the specified character in this string

(2) indexOf (String str) returns the index of the first occurrence of the substring in this String.

(3) indexOf (int ch, int fromIndex) returns the index of the specified character for the first time in this string, starting from the specified index

(4) indexOf (String str, int fromIndex) returns the index of the first occurrence of the substring, starting from the specified index.


2. Method example

/*** @ Title: IndexOf. java * @ Package: com. you. data * @ Description: Java indexOf () * @ Author: youhaidong * @ date: 7:31:58, January 1, March 17, 2014 * @ Version V1.2.3 */package com. you. data;/*** @ Class Name: IndexOf * @ Description: 1. public int indexOf (int ch) * returns the index of the first occurrence of a specified character in this String * If a character with a value of * ch appears in the String object, returns the index that appears for the first time (expressed in Unicode code units ). * For the ch value between 0 and 0 xFFFF (including 0 and 0 xFFFF), the return value is * this. charAt (k) = ch * 2. indexOf (String str) returns the Index * 3. indexOf (int ch, int fromIndex) of the first occurrence of the substring) returns the index that appears at the specified character for the first time in this string and starts searching from the specified index. * 4. indexOf (String str, int fromIndex) returns the index of the first occurrence of the substring from the specified index. * @ Author: You Haidong * @ date: 7:31:58, January 1, March 17, 2014 */public class IndexOf {/*** @ Title: main * @ Type: IndexOf * @ date: march 17, 2014 7:31:58 * @ Description: * @ param args */public static void main (String [] args) {/*** declare a String str */String str = "hjhfd? 7c8yu? 90uij? B $ % a $ ^ % ^ dnjdhf?? Ndjfhdj? Abdeefsdfdf? Ojkdjk837587133 ";/*** position where the first d appears */int strNum1 = str. indexOf (100);/*** print the location where the first d appears */System. out. println ("first indexOf:" + strNum1); System. out. println ("\ n" + "-------------------------------" + "\ n");/*** the position where the first a appears */int strNum2 = str. indexOf ("a");/*** print the location where the first a appears */System. out. println ("second indexOf:" + strNum2); System. out. println ("\ n" + "-------------------------------" + "\ n");/*** position where the first B appears */int strNum3 = str. indexOf (99, 2);/*** print the location where the first B appears */System. out. println ("third indexOf:" + strNum3); System. out. println ("\ n" + "-------------------------------" + "\ n");/*** position where the first B appears */int strNum4 = str. indexOf ("B", 34);/*** print the location where the first B appears */System. out. println ("fourth indexOf:" + strNum4 );}}

3. instance result

The first indexOf: 4 --------------------------------- the second indexOf: 21 ----------------------------------- the third indexOf: 7 ------------------------------- the fourth indexOf: 46


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.