Remove character from String

Hi! I am working on an assignment and have some problems with it.

I am given 2 strings

String iniColor ='GBGR';
String gColor='GGGB';

I have to compare the 2 strings character by character and delete them if they are the same.

For example, the first character of iniColor is G, the first character of gColor is also G. So iniColor will now become 'BGR' and gColor becomes 'GGB'.

Since now
iniColor ='BGR'; gColor='GGB';
it will again compare the first characters. This time, it is 'B' and 'G'. So it will move to the second character.

At the second character, both are G. so now
iniColor ='BR'; gColor='GB';

I don't know how am I going to achieve this by removing the characters from the string. I tried using substring, but it is not able to remove the middle character in a string, and retain the rest.

Can anyone help please? Thanks!
[1034 byte] By [lwings] at [2007-11-20 0:12:42]
# 1 Re: Remove character from String
To remove a character from a Java String you could do the following:

String S = "blabla";
....
// remove character from pos i:
String newS = S.substring(0,i)+S.substring(i+1);

Regards,
Theodore
yiannakop at 2007-11-10 2:18:53 >
# 2 Re: Remove character from String
If your assignment asks you to compare character by character then it's probably best to call the toCharArray() method on each string. This will give you two arrays of chars which you can iterate over comparing each pair of characters in turn. You could then store different chars in a pair of instances of StringBuilder classes by appending the chars as they are found. Finally call the toString() method on the StringBuilder objects to construct the new strings.

I tried using substring, but it is not able to remove the middle character in a string, and retain the rest.Actually you can do this using substring() but it involves two calls to substring and a concatenation which is not very efficient. You call substring to get the section of the string before the character to remove and then you call substring to get the part after the character to remove and finally you concatenate the two strings to create a new string without the character.
keang at 2007-11-10 2:19:53 >
# 3 Re: Remove character from String
Euwww.. you would use a stringbuffer for this, not strings as every manipulation of them causes a new string to be created..

Take a look at:
http://java.sun.com/j2se/1.3/docs/api/java/lang/StringBuffer.html

Fill 2 stringbuffers with the contents of your 2 strings
Then write a loop that skips over the buffers, comparing characters at same locations.
If the characters are the same, removeCharAt() on both buffers

NOTE! THink about this! Most skipping operations are forwards ops, but removeCharAt shortens the buffer. If you look at char 0, find they are the same, delete it then move on to look at what is now char 1... it used to be char2 before the delete, so you will find yourself skipping comparison of any char after a deleted one if youre not careful
to get round this, think carefully about where you increment your loop integer
OR, skip backwards :)

Have a go at implementing what i've specced here.. and we'll help you with it
cjard at 2007-11-10 2:20:56 >
# 4 Re: Remove character from String
Euwww.. you would use a stringbuffer for thiserr.. don't you mean StringBuilder rather than StringBuffer. The Java API Docs for StringBuilder say:
This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.
keang at 2007-11-10 2:22:02 >
# 5 Re: Remove character from String
If you remove chracter-by-character from StringBuffer it'll be not especially better then removing from String - it untimately calls System.arraycopy in my implementation of StringBuffer and it's what natural to expect. StringBuffer is good at append'ing, no more - it's conceptually ArrayList for primitive values.
What you need is to construct new strings while iterating old ones, appending different characters rather then removing equal. For this you can use arrays of reserved size. String str1 = "abcf";
String str2 = "adef";
assert(str1.length()==str2.length());
int nDiff = 0;
int length = str1.length();
char res1[] = new char[length];
char res2[] = new char[length];
for(int i=0;i<length;i++) {
char ch1 = str1.charAt(i);
char ch2 = str2.charAt(i);
if(ch1!=ch2) {
res1[nDiff]=ch1;
res2[nDiff]=ch2;
nDiff++;
}
}
String subStr1 = new String(res1,0,nDiff);
String subStr2 = new String(res2,0,nDiff);
RoboTact at 2007-11-10 2:23:01 >
# 6 Re: Remove character from String
If the characters are the same, removeCharAt() on both buffersThat should read deleteCharAt(..).

The real technology -behind all our other technologies- is language. It actually creates the world our consciousness lives in...
A. Codrescu
dlorde at 2007-11-10 2:24:03 >