0

I am trying to implement a logic where I need to replace particular sub-string(no of characters) in a string from given starting index to end index.

For example:

Suppose there is a string called "elephant" and I have given starting index as 2 and end index as 5 and i need to replace characters between those indexes with another given string "tiger". So the resultant string should be "eltigernt".

Similarly,Suppose there is a string called "elephant elephant tiger" and I have given starting index as 2 and end index as 5 and i need to replace characters between those indexes with another given string "tiger". So the resultant string should be "eltigernt elephant tiger".

Sritam Jagadev
  • 955
  • 4
  • 18
  • 46
  • 1
    what is stopping you? with some simple String concatenation and substring calls, this is far from difficult – Stultuske Jan 29 '18 at 09:53
  • Possible duplicate of [replace String with another in java](https://stackoverflow.com/questions/5216272/replace-string-with-another-in-java) – aja Jan 29 '18 at 10:25
  • it's not duplicate. This question is different. Please check – Sritam Jagadev Jan 29 '18 at 10:31

3 Answers3

0

Using the default String methods is the best way I suppose:

String s = "elephant";
String substitue = "tiger";
System.out.println(s.substring(0, 2) + substitute + s.substring(6));

Note that you have to increase the upper bound by 1. Also all the indexes have to be replaced by the variables.

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
  • no i can't use this logic. because it will replace all the occurrences of elephant string in a sentence. I need to replace string between given starting index and end index – Sritam Jagadev Jan 29 '18 at 09:57
  • Why do you `s.substring(2, 6).replace(s.substring(2, 6), substitue)`, you can simply use `substitue` since you replace the exact substring... `s.substring(0, 2) + substitue + s.substring(6)` where `2` is the start and `6` is the end + 1 – AxelH Jan 29 '18 at 10:47
0

You can either use StringBuilder or just simple for-loop through CharArray.

StringBuilder version:

public static String replace(int start, int end, String ori, String rep){
    StringBuilder sb = new StringBuilder();
    sb.append(ori,0,start)
            .append(rep)
            .append(ori,end+1,ori.length());
    return sb.toString();
}

Hope this helps :)

NOTE : Although you can use simple String "+", I recommend StringBuilder for better performance instead, in this case.

0

Just using the StringBuilder methods replace, you can defined the boundaries to replace and the specific value :

String s = "FooBar";
StringBuilder sb = new StringBuilder(s);
sb.replace(1,3,"##");
System.out.println(sb.toString());

f##bar

Documentation :

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.
AxelH
  • 14,325
  • 2
  • 25
  • 55