简体   繁体   中英

what working mechanism of replace() in java

I wrote java program using StringBuilder class.

 class StringHandling2
 {
 public static void main(String args[])
     {
       StringBuilder sb=new StringBuilder("Welcom");
       sb.replace(1,1,"JAVA");
       System.out.println(sb);
     }
 }

I got output that is WJAVAelcome

after i modified index value of replace() in this program,

class StringHandling2
{
   public static void main(String args[])
    {
        StringBuilder sb=new StringBuilder("Welcom");
        sb.replace(2,1,"JAVA");
        System.out.println(sb);
    }
}

jvm throws runtime error that is StringIndexOutofBoundException.

how is replace(int startIndex, int endIndex,string) working in that program?

public StringBuilder replace(int start, int end, String str)

Replaces the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character exists. First the characters in the substring are removed and then the specified String is inserted at start. (This sequence will be lengthened to accommodate the specified String if necessary.)

Parameters:

 start - The beginning index, inclusive.<br> end - The ending index, exclusive.<br> str - String that will replace previous contents.<br> 

Returns:

 This object.<br> 

Throws:

 StringIndexOutOfBoundsException - **if start is negative, greater than length(), or greater than end**. 

( source )

sb.replace(2,1,"JAVA");

2 > 1, hence the exception.

It replaces the characters between the start- and end index you specify with the string given in the third argument.

The error is this case comes from the fact that your startindex can't be greater then your end index.

More info can be found here

This is very common concept; according to JavaDoc of replace() method

public StringBuilder replace(int start,
                int end,
                String str)

Replaces the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character exists. First the characters in the substring are removed and then the specified String is inserted at start. (This sequence will be lengthened to accommodate the specified String if necessary.) Parameters:

start - The beginning index, inclusive.

end - The ending index, exclusive.

str - String that will replace previous contents.

Returns: This object.

Throws:

StringIndexOutOfBoundsException - if start is negative, greater than length(), or greater than end.

In your case the start integer is greater that start which would obviously throw ArrayOutOfBoundException.

and if you are curious why so then the AbstractStringBuilder class is used to replace the characters and this is achieved by creating array of characters in background which is hidden from us. 并且如果您很好奇为什么这么做,那么可以使用AbstractStringBuilder类来替换字符,这是通过在背景中创建对我们隐藏的字符数组来实现的。

Good question.

StringBuilder.replace(int start, int end, String s) replaces characters from start index till 1 less than end index by String s.

For eg.

StringBuilder str = new StringBuilder("hello world");
str.replace(0,1,"welcome"); // o/p -welcomeello world
str.replace(2,5,"welcome"); // o/p - hewelcome world

i guess it internally converts string into char[] charArray & replaces char[start] to char[end-1] with string argument value.

So your always end has to greater than start

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM