Results 1 to 3 of 3

Thread: substring replace

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Massachusetts, USA
    Posts
    111

    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";

  2. #2
    Look in the JDK documentation for the String class.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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
  •  



Click Here to Expand Forum to Full Width