Results 1 to 7 of 7

Thread: A new .replace() function for the String class

  1. #1

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935

    A new .replace() function for the String class

    VB has the Replace method for strings where you pass it two strings, one with the text to find, and one with the text to replace it with.

    Java has the same thing but it only works on a per-character basis; how can I make one that uses Strings instead?

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Yes your right. public String replace(char oldChar,char newChar)
    I guess java does this because the length of the String is set when initalized and thus remains immutable.

    Why not just use the String Buffer version of the replace function? public StringBuffer replace(int start, int end, String str)

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Code:
    StringBuffer sb = new StringBuffer("Java");
    sb = sb.replace(0,1,"Gu");
    String s = new String(sb);
    Im not sure if the start and end is inclusive or exclusive so i will have to test this. I tried pulling up my JCreator but i was getting an invalid path error, but you get the idea.

  4. #4

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    I don't get it: it only takes one string; shouldn't it has for two, for a to find and to replace with, or am I misreading the Javadocs that are so cryptically written?

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Posted by Filbert
    I don't get it: it only takes one string; shouldn't it has for two, for a to find and to replace with, or am I misreading the Javadocs that are so cryptically written?
    Wait hold........ ok I had a lot to drink last night so right now i feel like sponge bob that cartoon character. No that should be correct. The sb.replace() method should have an internal pointer that points to the value which is contained within the StringBuffer. Oh i see what you want. You want to replace a certian character with another character without having to figure out the offset. It seems the the replace method of the String class works in this manner replacing oldChar with newChar. The replace method of the StringBuffer class works diffrently. You have to know the index of the char within the StringBuffer so you can replace it.

  6. #6

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    1. It doesn't matter if the two strings are of different lengths?
    2. So all I have to do is iterate through the string one char at a time replacing as I find occurances by using the StringBuffer's Replace method?

  7. #7
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    So all I have to do is iterate through the string one char at a time replacing as I find occurances by using the StringBuffer's Replace method?
    I guess if you were just replacing on a char by character basis then the int start and int end arguements would just be the same all the time (the index of the char that you wish to replace)

    I don't this it would be necessary to iterate the String. As long as you know the index of the char that you would like to replace then this method would only have to be called once.

    For instance, say if you get the index of the character you want to replace with the public int indexOf(int char) method. Then use that value as the start and end arguements in the replace method.

    replace(int start, int end, String str)

    Im sure you could easly code a method the takes an index value and a char as an arguement, replaces the char and returns a String.

    public static String(int index, char chartoreplace){}

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