|
-
Aug 18th, 2001, 07:25 PM
#1
Thread Starter
Lively Member
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";
-
Aug 18th, 2001, 08:17 PM
#2
Member
Look in the JDK documentation for the String class.
-
Aug 19th, 2001, 01:39 PM
#3
Dazed Member
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);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|