- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java.lang.String.indexOf() Method
Description
The java.lang.String.indexOf(int ch) method returns the index within this string of the first occurrence of the specified character.
If a character with value ch occurs in the character sequence represented by this String object, then the index(Unicode code units) of the first such occurrence is returned.
Declaration
Following is the declaration for java.lang.String.indexOf() method
public int indexOf(int ch)
Parameters
ch − This is a character (Unicode code point).
Return Value
This method returns the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.
Exception
NA
Example
The following example shows the usage of java.lang.String.indexOf() method.
package com.tutorialspoint; import java.lang.*; public class StringDemo { public static void main(String[] args) { String str = "This is tutorialspoint"; // returns the index of occurrence of character s System.out.println("index of letter 's' = " + str.indexOf('s')); // returns -1 as character e is not in the string System.out.println("index of letter 'e' = " + str.indexOf('e')); } }
Let us compile and run the above program, this will produce the following result −
index of letter 's' = 3 index of letter 'e' = -1
java_lang_string.htm
Advertisements
To Continue Learning Please Login
Login with Google