- 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.StringBuilder.indexOf() Method
Description
The java.lang.StringBuilder.indexOf(String str, int fromIndex) method returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
Declaration
Following is the declaration for java.lang.StringBuilder.indexOf() method
public int indexOf(String str, int fromIndex)
Parameters
str − This is the substring for which to search.
fromIndex − This is the index from which to start the search.
Return Value
This method returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
Exception
NullPointerException − if str is null.
Example
The following example shows the usage of java.lang.StringBuilder.indexOf() method.
package com.tutorialspoint; import java.lang.*; public class StringBuilderDemo { public static void main(String[] args) { StringBuilder str = new StringBuilder("programming language"); System.out.println("string = " + str); // returns the index of the specified substring System.out.println("Index of substring = " + str.indexOf("age")); /* returns the index of the specified substring, starting at the specified index */ System.out.println("Index of substring = " + str.indexOf("am",2)); /* returns -1 as the substring is not found starting at the specified index */ System.out.println("Index of substring = " + str.indexOf("am",10)); } }
Let us compile and run the above program, this will produce the following result −
string = programming language Index of substring = 17 Index of substring = 5 Index of substring = -1
java_lang_stringbuilder.htm
Advertisements