+ 3
Can somebody help me with this stringindexoutofboundexception in this code?
I want to do find and replace word in a sentence without using replace method but I am stuck with this exception can somebody help me with this https://code.sololearn.com/cc3xpXNWWIai/?ref=app
3 Answers
+ 3
Viki Have look if this works:
https://code.sololearn.com/cn5nWBpFwFsd/?ref=app
+ 3
Ya,it works. đđtq buddy.
+ 2
Your program works fine if the words have the same length. :)
I think you need to calculate the difference between find.length() and replace.length()
int a = str.length();
int b = find.length();
int c = replace.length();
--> a - b - c
Or:
int diff = (int)Math.abs(find.length() - replace.length())
for(int i = 0;i < a - diff;i++)
{
Math.abs() --> absolute value --> so you don't have to care about if you replace a smaller or larger wors then your given word.