Ok, so I tried with your help to learn a little bit more about strings and Chaining Strings in Java.
now I know that strings are immutable, but I'm having a really hard time to do this ex:
Implement the method
public static String notReplace(String str)The method takes a String as its input and returns a String in which every occurrence of the lowercase word "is" has been replaced with "is not". The word "is" should not 2 be immediately preceded or followed by a letter -- so for example the "is" in "this" should not be replaced. (Note:
Character.isLetter(char)tests if a char is a letter.)
Examples:
notReplace("is test") → "is not test"
notReplace("is-is wise") → "is not-is not wise"
This is what I wrote:
public class NotReplace{
public static void main(final String[] args){
final String str2 = "is no";
System.out.println(notReplace(str2));
}
public static String notReplace(final String str){
final int times = str.length();
final StringBuilder sb = new StringBuilder(str);
for(int i = 0; i <= times; i++){
if((str.charAt(i) == 'i') && (str.charAt(i + 1) == 's')
&& !Character.isLetter(str.charAt(i + 2))){
sb.insert(i, "not");
}
final String str1 = sb.toString();
return str1;
}
}
}
I believe it is a complete mess, I'll be happy to learn more how to work with strings in situations like this.
Thanks
Edit: I can't use replaceAll function.
i<=timesis vastly incorrect