0

I am just wondering if there is any difference between the semantics of offset and index when indicating the location where we can insert the specified argument (be it of type String, Object, char[], etc). For example, all of the two-argument insert() overloaded methods refer to that location as "offset". However, this one:

public StringBuilder insert(int index, char[] str, int offset, int len)

uses "index" to refer to the position in the string where the insertion takes place. Technically, I believe the correct term should be "offset" but just wondering why two different terms have been used by the class designer(s) for, basically, the same action.

Thank you.

soti84
  • 83
  • 1
  • 10
  • Generally I believe I see ‘index’ used about positions in general, while ‘offset’ is most often used together with ‘length’ (or ‘len’, as here) to indicate a range of positions. You are right, of course, that the `insert` methods of `StringBuilder` don’t follow this usage consequently. – Ole V.V. Aug 31 '16 at 08:40
  • The method signatures have been taken over from `StringBuffer`. Which doesn’t answer your question, of course. – Ole V.V. Aug 31 '16 at 08:45

2 Answers2

0

From javadoc:

Inserts the string representation of a subarray of the str array argument into this sequence. The subarray begins at the specified offset and extends len chars. The characters of the subarray are inserted into this sequence at the position indicated by index. The length of this sequence increases by len chars.

So:

  • offset is related to str, the char[] that you are adding to the content of the StringBuilder
  • index is related to the actual content in the StringBuilder

Specifically from javadoc:

index - position at which to insert subarray.

str - A char array.

offset - the index of the first char in subarray to be inserted.

len - the number of chars in the subarray to be inserted.


Here is a sample code showing it:

    String originalContent = "1234567890";
    char[] charArray = new char[]{'a', 'b', 'c', 'd'};

    StringBuilder a = new StringBuilder(originalContent);
    a.insert(2, charArray, 0, 1);
    System.out.println(a.toString()); // Prints 12a34567890

    StringBuilder b = new StringBuilder(originalContent);
    b.insert(0, charArray, 2, 1);
    System.out.println(b.toString());   // Prints c1234567890
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • Hello Davide, have a look at the rest of the two-argument overloaded insert() methods. "offset" is used to refer to the actual content in the StringBuilder, not "index". – soti84 Aug 31 '16 at 08:28
  • @soti84 you can't look a description of another method. In any case it seems that the developer of this class used a wrong name choice for one of the two methods. The description for each of them are correct. – Davide Lorenzo MARINO Aug 31 '16 at 09:18
0

Offset is just the position where in you will be inserting the new String or Character that you will be adding in .

Here is the Example : I'm using the 2 argument format for the insert() method.

public StringBuilder insert(int offset, String str)  

Here is the below code I tried and corresponding Output.

public class Main
{
    public static void main(String[] args) {
        System.out.println("Hello World");
        StringBuilder sb = new StringBuilder();
        String str = "HelloJoinme";
        String str2 = "123456";
        sb.insert(0,str);
        System.out.println(sb.toString());
        sb.insert(2,str2);
        System.out.println(sb.toString());
    }
}

And the Ouptut. Output

Ouput in Text Format :

Hello World
HelloJoinme
He123456lloJoinme