-
substring replace
i need to use a method with similar functionality to PHP's substr_replace.
example:
example=substr_replace("this is a test","joke",10,4);
example contains "this is a joke";
example2:
example2=substr_replace("this is a test","joke",10,0);
example2 contains "this is a joketest";
-
Look in the JDK documentation for the String class.
-
You would probably want to use a StringBuffer instead.
The replace method of the String class is public String(char oldchar, char newchar) and the replace mathod of the StringBuffer class is public StringBuffer(int start, int end, String str)
public class Test{
public static void main(String[] args){
StringBuffer greeting = new StringBuffer("This is a test");
StringBuffer example2 = greeting.replace(10,14,"joke");
System.out.println(example2);
}
}
This is a joke.........
you can also insert by using the StringBuffer insert method:
There are too many to list but the one most used is public StringBuffer insert (int offset, String str);