
- 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
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java.lang.StringBuffer.indexOf() Method
Description
The java.lang.StringBuffer.indexOf(String str) method returns the index within this string of the first occurrence of the specified substring.
Declaration
Following is the declaration for java.lang.StringBuffer.indexOf() method
public int indexOf(String str)
Parameters
str − This is any string.
Return Value
if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.
Exception
NullPointerException − if str is null
Example
The following example shows the usage of java.lang.StringBuffer.indexOf() method.
package com.tutorialspoint; import java.lang.*; public class StringBufferDemo { public static void main(String[] args) { StringBuffer buff = new StringBuffer("programming language"); System.out.println("buffer = " + buff); // returns the index of the specified substring System.out.println("Index of substring = " + buff.indexOf("age")); // returns -1 as substring is not found System.out.println("Index of substring = " + buff.indexOf("k")); } }
Let us compile and run the above program, this will produce the following result −
buffer = programming language Index of substring = 17 Index of substring = -1
java_lang_stringbuffer.htm
Advertisements