|
-
Dec 13th, 2001, 10:12 PM
#1
Thread Starter
Member
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?
-
Dec 14th, 2001, 02:08 PM
#2
Dazed Member
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)
Last edited by Dilenger4; Dec 14th, 2001 at 02:12 PM.
-
Dec 14th, 2001, 02:15 PM
#3
Dazed Member
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.
Last edited by Dilenger4; Dec 14th, 2001 at 02:23 PM.
-
Dec 14th, 2001, 02:27 PM
#4
Thread Starter
Member
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?
-
Dec 14th, 2001, 02:39 PM
#5
Dazed Member
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.
-
Dec 14th, 2001, 02:42 PM
#6
Thread Starter
Member
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?
-
Dec 14th, 2001, 02:54 PM
#7
Dazed Member
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){}
Last edited by Dilenger4; Dec 14th, 2001 at 03:04 PM.
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
|