I know this is an old question, however I have written a extension method that performs an IndexOf on a StringBuilder. It is below. I hope it helps anyone that finds this question, either from a Google search or searching StackOverflow.

/// <summary>
/// Returns the index of the start of the contents in a StringBuilder
/// </summary>        
/// <param name="value">The string to find</param>
/// <param name="startIndex">The starting index.</param>
/// <param name="ignoreCase">if set to <c>true</c> it will ignore case</param>
/// <returns></returns>
public static int IndexOf(this StringBuilder sb, string value, int startIndex, bool ignoreCase)
{            
    int index;
    int length = value.Length;
    int maxSearchLength = (sb.Length - length) + 1;

    if (ignoreCase)
    {
        for (int i = startIndex; i < maxSearchLength; ++i)
        {
            if (Char.ToLower(sb[i]) == Char.ToLower(value[0]))
            {
                index = 1;
                while ((index < length) && (Char.ToLower(sb[i + index]) == Char.ToLower(value[index])))
                    ++index;

                if (index == length)
                    return i;
            }
        }

        return -1;
    }

    for (int i = startIndex; i < maxSearchLength; ++i)
    {
        if (sb[i] == value[0])
        {
            index = 1;
            while ((index < length) && (sb[i + index] == value[index]))
                ++index;

            if (index == length)
                return i;
        }
    }

    return -1;
}
Answer from Dennis on Stack Overflow
🌐
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)
🌐
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 - The .indexOf() method returns the index of the first occurrence of a substring in a StringBuilder.
🌐
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
🌐
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);
🌐
Medium
medium.com › @anantsingh3346 › string-stringbuffer-and-stringbuilder-32273b971d9f
String, StringBuffer and StringBuilder | by Anant Singh | Medium
January 22, 2023 - · indexOf(char)-this method takes a character as a parameter and returns the first index position of the character.
Find elsewhere
🌐
W3Schools
w3schools.com › java › ref_string_indexof.asp
Java String indexOf() Method
The indexOf() method returns the position of the first occurrence of specified character(s) in a string.
🌐
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....
🌐
Scala Programming Language
scala-lang.org › api › current › scala › collection › mutable › StringBuilder.html
StringBuilder
StringBuilder.scala · def indexOf(str: String, fromIndex: Int): Int · Finds the index of the first occurrence of the specified substring. Finds the index of the first occurrence of the specified substring. fromIndex · the smallest index in the source string to consider ·
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
April 21, 2026 - The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the ...
🌐
Stack Overflow
stackoverflow.com › questions › 17076927 › misleading-javadoc-comment-on-stringbuilder-indexof
java - Misleading javadoc comment on StringBuilder indexOf? - Stack Overflow
CopyStringBuilder sb = new StringBuilder("Example"); System.out.println(sb.indexOf("", 1234)); //Outputs sb.length(), which is 7.
🌐
ByteHide
bytehide.com › blog › indexof usage in c#: tutorial
IndexOf Usage in C#: Tutorial (2026)
December 19, 2023 - Isn’t that sweet? The StringBuilder class doesn’t have an IndexOf method in its repository, but you can use the ToString method to convert the object into a string and use IndexOf there.
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuffer-indexof-method-in-java-with-examples
StringBuffer indexOf() method in Java with Examples - GeeksforGeeks
April 18, 2023 - The indexOf(String str, int fromIndex) method of StringBuffer class is used to return the index within the String for first occurrence of passed substring starting from the specified index 'fromIndex'.
Top answer
1 of 3
3

A practical reason is that it makes things easier in some common situations:

  • If you want the operation to include everything until the end of the string, you can directly use the length as endIndex
  • If you have "separation characters", like the dot between base name and filetype suffix, you typically don't want to include them, so you can use the index of the separation character as endIndex, e.g. String basename = filename.substring(0, filename.indexOf('.'))
2 of 3
1

I agree with Micheal Borgwardt's answer but there is an opportunity for elaboration. The approach you see here is not only useful but also consistent with most the APIs you will find in Java and many other languages. The association with 0-based indexing is more clear if you think about the standard old-style for-loop: for (int i = 0; i < end; i++). These days we tend to favor for-each style constructs but this is how a lot of code is written. Note the while condition, it's based on and exclusive end with a < instead of a <=. When you are writing a lot of loops like this, it's helpful to use a similar construct each time.

But to really understand the bigger picture on this, I think it helps to look at a different kind of problem: time intervals. Let's imagine I want to tell if something happened today, in the morning. If I structure this as an inclusive end, I then have a problem of figuring out what the last time possibly could be. That means I need to know the smallest resolution of the clock. Is it seconds, milliseconds, microseconds, nanoseconds? Let's say I think seconds is good enough. So I say 'less than or equal to 11:59'. Does that work if it happened at 11:59 and 30 seconds? Well, I'll check whether "the minute of the day was less than or equal to 11:59". Or I could just say "less than noon" which is a lot more elegant and tends to be more robust.

Or consider date periods. How do I know if two date periods are adjacent i.e. that there is no gap in between? If I used an exclusive end, it's trivial. I just check that the (exclusive) end on the first period is equal to or after the (inclusive) start of the second. If I use an inclusive end, now I have to get out a calendar and figure out if the day after September 30th is October 1st. And does March 1st follow Feb 28? What year is it? Divide that by 4 unless we are talking about the last year of the century but don't forget that the year 2000 is the exception to the exception. There are strategies to solve that kind of thing but the easiest is to convert the inclusive end to exclusive.

🌐
Coderanch
coderanch.com › t › 669328 › java › StringBuilder-substring
StringBuilder and substring() [Solved] (Beginning Java forum at Coderanch)
Different indexOf signatures in String & StringBuilder (Java OCA 8 Programmer I Study Guide, Sybex) Decompressing a String Recursively · App chokes when SAX-parsing an XML file without a DTD file ·
🌐
Java Guides
javaguides.net › 2024 › 06 › java-stringbuilder-indexof-method.html
Java StringBuilder indexOf() Method
June 10, 2024 - The StringBuilder.indexOf() method in Java is used to find the index of the first occurrence of a specified substring within a StringBuilder object. This guide will cover both overloaded versions of the method, explain how they work, and provide ...
🌐
Java Tutorial HQ
javatutorialhq.com › java tutorial › java.lang › stringbuilder › indexof() method example
Java StringBuilder indexOf() method example
September 30, 2019 - The above java example source code demonstrates the use of indexOf() method of StringBuilder class. 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.
🌐
GitHub
github.com › dotnet › runtime › issues › 80060
[API Proposal]: StringBuilder.IndexOf(string find, int start) · Issue #80060 · dotnet/runtime
December 30, 2022 - var builder = new StringBuilder("Text to Find"); var offset = builder.IndexOf("Find");
Author   dotnet