You can use String.valueOf

builder.indexOf(String.valueOf(c));

There are good things about this approch.

  1. Clean code
  2. String.valueOf creates String object using char[] from passed char likechar data[] = {c}; so no additional operation is required.

2 is really a micro optimization and I would always go for option 1 i.e. "clean code".

For what it's worth, here's the bytecode generated by the concatenation version:

new #2; //class java/lang/StringBuilder
dup
invokespecial #6; //Method java/lang/StringBuilder."<init>":()V
aload_1
invokevirtual #7; //Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
ldc #8; //String 
invokevirtual #9; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
invokevirtual #10; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
invokevirtual #11; //Method java/lang/StringBuilder.indexOf:(Ljava/lang/String;)I

As you can see, it creates a second StringBuilder, does two append calls, and then a toString. In contrast, here's the String.valueOf version:

aload_0
aload_1
invokestatic #12; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
invokevirtual #11; //Method java/lang/StringBuilder.indexOf:(Ljava/lang/String;)I

That just hands the Character (which has already been automatically unboxed into a char) off to String.valueOf. So what does that do? Let's look at the JDK source code:

public static String valueOf(char c) {
    char data[] = {c};
    return new String(0, 1, data);
}

So it creates a new one-character array and hands off directly to the String constructor. Very likely to be more efficient.

But again, it's probably a micro-optimization. The String.valueOf call makes the code clearer, which is the main thing.

Answer from Amit Deshpande on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuilder-indexof-method-in-java-with-examples
StringBuilder indexOf() method in Java with Examples - GeeksforGeeks
April 18, 2023 - The indexOf(String str, int fromIndex) method of StringBuilder class is the inbuilt method used to return the index within the String for first occurrence of passed substring as parameter starting at the specified index 'fromIndex'.
🌐
Codecademy
codecademy.com › docs › java › stringbuilder › .indexof()
Java | StringBuilder | .indexOf() | Codecademy
August 22, 2022 - Returns the index of the first occurrence of a substring in the StringBuilder or -1 if none are found.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › stringbuilder_indexof_str.htm
Java StringBuilder indexOf() Method
The Java StringBuilder indexOf() method is used to retrieve the index of the first occurrence of the specified character in a String or StringBuilder, and the method is case-sensitive, which means that the string "A" and "a" are two different values.
Top answer
1 of 2
6

You can use String.valueOf

builder.indexOf(String.valueOf(c));

There are good things about this approch.

  1. Clean code
  2. String.valueOf creates String object using char[] from passed char likechar data[] = {c}; so no additional operation is required.

2 is really a micro optimization and I would always go for option 1 i.e. "clean code".

For what it's worth, here's the bytecode generated by the concatenation version:

new #2; //class java/lang/StringBuilder
dup
invokespecial #6; //Method java/lang/StringBuilder."<init>":()V
aload_1
invokevirtual #7; //Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
ldc #8; //String 
invokevirtual #9; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
invokevirtual #10; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
invokevirtual #11; //Method java/lang/StringBuilder.indexOf:(Ljava/lang/String;)I

As you can see, it creates a second StringBuilder, does two append calls, and then a toString. In contrast, here's the String.valueOf version:

aload_0
aload_1
invokestatic #12; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
invokevirtual #11; //Method java/lang/StringBuilder.indexOf:(Ljava/lang/String;)I

That just hands the Character (which has already been automatically unboxed into a char) off to String.valueOf. So what does that do? Let's look at the JDK source code:

public static String valueOf(char c) {
    char data[] = {c};
    return new String(0, 1, data);
}

So it creates a new one-character array and hands off directly to the String constructor. Very likely to be more efficient.

But again, it's probably a micro-optimization. The String.valueOf call makes the code clearer, which is the main thing.

2 of 2
0

The String and StringBuilder/StringBuffer classes in Java uses brute force method of pattern first exact string matching algorithm. This algorithm involves checking for an occurrence of the pattern at all positions of the text starting at some position and moving by one position, so I feel String.indexOf and StringBuilder.indexOf will be identical in terms of performance.

However, the String class has a indexOf(char c) method while StringBuilder/StringBuffer have indexOf(String s) methods, so if you searching for a character in StringBuilder you will have to first cast the char to String. In your example, that would probably be the only overhead.

Note: The brute force algorithm employed by String and StringBuilder classes are enough when you are dealing with small or medium sized texts but when dealing with large documents, it is preferable to use more sophisticated search string search algorithms.

🌐
Tutorialspoint
tutorialspoint.com › java › lang › stringbuilder_indexof_str_index.htm
Java.lang.StringBuilder.indexOf() Method
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.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.stringbuilder.indexof
StringBuilder.IndexOf Method (Java.Lang) | Microsoft Learn
[Android.Runtime.Register("indexOf", "(Ljava/lang/String;I)I", "")] public override int IndexOf(string str, int fromIndex);
🌐
IncludeHelp
includehelp.com › java › stringbuilder-indexof-method-with-example.aspx
Java StringBuilder indexOf() method with example
// Java program to demonstrate the example // of indexOf () method of StringBuilder class public class IndexOf { public static void main(String[] args) { // Creating an StringBuilder object StringBuilder st_b = new StringBuilder("Java World "); // Display st_b System.out.println("st_b =" + st_b); // By using indexOf("a") method is to return the first index of // given string "a" in st_b object // (first a at index 1 and second a at index 3) // it returns 1 int index1 = st_b.indexOf("a"); // Display st_b index System.out.println("st_b.indexOf(String) =" + index1); // By using indexOf("a",1) method is to return the first index of // given string "a" in st_b object // (first a at index 1 and second a at index 3) // it returns 1 and searching starts at index 1 int index2 = st_b.indexOf("a", 1); // Display st_b index System.out.println("st_b.indexOf(String, int) =" + index2); } } Output ·
🌐
Java Tutorial HQ
javatutorialhq.com › java tutorial › java.lang › stringbuilder › indexof() method example
Java StringBuilder indexOf() method example
September 30, 2019 - Initially the code assigns a string “javatutorialhq.com” as initial contents of the StringBuilder. Then we use the indexof method to get the starting index of string “a”. This is very useful method that works well with substring method of StringBuilder class.
Find elsewhere
🌐
TutorialKart
tutorialkart.com › java › java-stringbuilder-indexof
Java StringBuilder.indexOf() - Syntax & Examples
January 24, 2021 - We will use indexOf(str, fromIndex) to find the index of first occurrence of this string in StringBuilder sequence from given index.
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-stringbuilder-indexof
Java StringBuilder indexOf()
October 4, 2022 - public class JavaExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder("Cool Book"); System.out.println("Given String: " + sb); // first occurrence of string "oo" System.out.println("Index of string 'oo': "+sb.indexOf("oo")); } }
🌐
Codecademy
codecademy.com › docs › java › stringbuilder › .lastindexof()
Java | StringBuilder | .lastIndexOf() | Codecademy
August 22, 2022 - Returns the index of the last (rightmost) occurrence of a substring in the StringBuilder or -1 if no substring is found.
🌐
Java Guides
javaguides.net › 2024 › 06 › java-stringbuilder-indexof-method.html
Java StringBuilder indexOf() Method
June 10, 2024 - The StringBuilder.indexOf() method in Java provides a way to find the index of the first occurrence of a specified substring within a StringBuilder object. By understanding how to use both overloaded versions of this method, you can efficiently ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 8 )
April 21, 2026 - public StringBuilder insert(int index, char[] str, int offset, int len)
🌐
Dremendo
dremendo.com › java-programming-tutorial › java-stringbuilder
StringBuilder in Java Programming | Dremendo
import java.util.Scanner; public ... } } ... The indexOf() method returns the index of the first occurrence of the specified character or string within the current string builder object....
🌐
Scaler
scaler.com › home › topics › java › stringbuilder in java with examples, methods and constructors
StringBuilder in Java with Examples, Methods, and Constructors - Scaler Topics
May 3, 2023 - This method is used for finding ... int indexof(String): This method returns the first index of the given String in the sequence if it is present, Otherwise this method returns -1 that indicates String is not present in the StringBuilder sequence....
🌐
Stack Overflow
stackoverflow.com › questions › 17076927 › misleading-javadoc-comment-on-stringbuilder-indexof
java - Misleading javadoc comment on StringBuilder indexOf? - Stack Overflow
Edit: as pointed below this is the comment from the Java 7 javadoc; java 6 has the right comment. ... Save this answer. ... Show activity on this post. It's a mistake. It's supposed to be this.length() instead of str.length(). That allows for fromIndex to be greater than this.length() in the case where str is empty. ... CopyStringBuilder sb = new StringBuilder("Example"); System.out.println(sb.indexOf("", 1234)); //Outputs sb.length(), which is 7.
🌐
Oracle
docs.oracle.com › en › java › javase › 26 › docs › api › java.base › java › lang › StringBuilder.html
StringBuilder (Java SE 26 & JDK 26)
March 16, 2026 - indexOf · (String str, int fromIndex) Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. StringBuilder · insert · (int offset, boolean b) Inserts the string representation of the boolean argument into this sequence.
🌐
Jnior
jnior.com › jnior › janos-runtime-javadoc › java › lang › StringBuilder.html
StringBuilder (JanosRuntime JavaDOC)
public StringBuilder insert(int ... to represent · Returns: this object · public int indexOf(String str) Returns the index within this string of the first occurrence of the specified substring....
🌐
Medium
medium.com › @anantsingh3346 › string-stringbuffer-and-stringbuilder-32273b971d9f
String, StringBuffer and StringBuilder | by Anant Singh | Medium
January 22, 2023 - · charAt(int)-this method takes an index of a string and returns its corresponding character of the string. · indexOf(char)-this method takes a character as a parameter and returns the first index position of the character.