Search within StringBuffer

In this chapter you will learn:

  1. How to search within StringBuffer
  2. How to search a StringBuffer from the end

How to search within StringBuffer

We can use the following two methods to search within a StringBuffer.

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

The following code shows how to search a within a StringBuffer.

public class Main{
  public static void main(String args[]) {
    StringBuffer sb = new StringBuffer();
    sb.append("java2s.com");
    int index = sb.indexOf("a");
    System.out.println(index);/* j a va2s  .c o m*/
    System.out.println(sb.toString());
    
  }
}

Search a StringBuffer from the end

The following two methods search a substring within StringBuffer from the end.

  • int lastIndexOf(String str)
    returns the index of the rightmost occurrence of the specified substring.
  • int lastIndexOf(String str, int fromIndex)
    returns the index of the last occurrence of the specified substring.
public class Main {
/*from  j a va2  s. c o m*/
  public static void main(String[] arg) {
    StringBuffer phrase = new StringBuffer("one two three four");
    int position = phrase.lastIndexOf("three");

    System.out.println(position);
  }

}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to convert portion of a StringBuffer to String
  2. How to convert StringBuffer to String
Home » Java Tutorial » String

String

    Java String type
    Java String Concatenation
    Java String Creation
    Java String Compare
    Java String Search
    Java String and char array
    Java String Conversion
    String trim, length, is empty, and substring
    String replace

StringBuffer

    StringBuffer class
    StringBuffer Insert and Append
    StringBuffer length and capacity
    StringBuffer char operation
    StringBuffer Operations
    Search within StringBuffer
    StringBuffer to String

StringBuilder

    StringBuilder
    StringBuilder insert and append
    StringBuilder length and capacity
    StringBuilder get,delete,set char
    StringBuilder delete, reverse
    StringBuilder search with indexOf and lastIndexOf
    StringBuilder to String

String Format

    Formatter class
    Format Specifier
    Format String and characters
    Format integer value
    Format decimal
    Scientific notation format
    Format octal and hexadecimal value
    Format date and time value
    Escape Formatter
    Minimum Field Width
    Specifying Precision
    Format Flags
    Uppercase Option
    Formatter Argument Index
    Align left and right
    Left and right padding a string

String Format Utilities

    Abbreviate string
    Caplitalize a string
    Uncapitalize a string
    Utility class for right padding
    Left padding
    Centers a String
    Transforms words