Results 1 to 7 of 7
Thread: Subtracting Strings
- 03-29-2009, 04:13 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
Subtracting Strings
Hi, I have been trying to figure out how to subtract one string from another to no avail.
This is NOT simple checking the strings for an incorrect sequence which can be done using string.replaceAll(...)
However, the thing I need to do is take any random string and remove from it any random number of letters.
For instance:
string s1 = computerscience
string s2 = tpu
string s3 = cpu
how can I do it so that s1-s2 = comerscience or s1-s3 = omterscience
Basically this is just removing the characters of 1 string from that of another. (But not removing all instances of letter, just as many as there are in the smaller string)
Can anybody help?
- 03-29-2009, 04:26 PM #2
There are many ways to go about this...
- You can do it by comparing each letter (s2 or s3) against each letter of s1 and build a new string. If comparasion is equal skip the letter in s1 otherwise add to the new string.
- or you can look into the StringBuilder methods (A mutable sequence of characters) to figure out how to manipulate S1.
StringBuilder (Java Platform SE 6)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-29-2009, 04:53 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
Thanks CJSL. I took a look at the String Builder methods and I have no idea how to use it. lol. I have never even heard of that class before. Your first option is very doable although it is somewhat inefficient. For instance, for each letter you compare, you skip the letter if it's equal thereby creating a new string. But you are literally doing that for each letter. Now this is alright if it was just a few comparisons. But if you are comparing this about 175,000 times or more, it becomes inefficient.
Is there any other way to do this? Is there any possible way of like mapping out certain letters from a string? Any ideas? Aside from CJSL's first option, Im stumped
- 03-29-2009, 05:19 PM #4
StingBuilder vs String
FYI... StringBuilder (and StringBuffer) are mutable (changable) sequence of characters, where as the String class is inmutable.
There is no method that will do what you want in one sweeping blow. Now, with that said, have you looked into the String methods of indexOf()? That will tell you if the character you are looking for is in the string and will return the index of the found character. That should help you a little.
String (Java Platform SE 6)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-29-2009, 05:24 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
Whoa. Thanks. Didn't think about that.
thanks thanks thanks.
If anyone has any other ideas. feel free to post. I am always interested in finding out other ways.
I'll post the code I implement using what CJSL said after i get my code working ^_^
- 03-29-2009, 05:34 PM #6
Some pseudo code to think about
Maybe something like:
Luck,Java Code:string newString; start loop1 select character to search for start loop2 indexOf() find an ocurrence of character ? if yes newString = newString + substring(found index) end loop2 end loop1
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-29-2009, 10:05 PM #7
Similar Threads
-
Subtracting Strings
By ravian in forum New To JavaReplies: 7Last Post: 10-08-2009, 06:26 PM -
Comparing Strings
By souFrag in forum Advanced JavaReplies: 5Last Post: 05-21-2008, 09:03 AM -
characters + strings
By Gilgamesh in forum New To JavaReplies: 3Last Post: 03-02-2008, 09:10 PM -
Comparison of Strings
By Cero.Uno in forum New To JavaReplies: 3Last Post: 02-11-2008, 02:46 AM -
Help with drawing strings!
By JavaInLove in forum AWT / SwingReplies: 1Last Post: 02-05-2008, 03:39 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks