Results 1 to 7 of 7
  1. #1
    lazygun247 is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default 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?

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    lazygun247 is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    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

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    lazygun247 is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    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 ^_^

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Some pseudo code to think about

    Maybe something like:

    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
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,238
    Rep Power
    19

Similar Threads

  1. Subtracting Strings
    By ravian in forum New To Java
    Replies: 7
    Last Post: 10-08-2009, 06:26 PM
  2. Comparing Strings
    By souFrag in forum Advanced Java
    Replies: 5
    Last Post: 05-21-2008, 09:03 AM
  3. characters + strings
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 03-02-2008, 09:10 PM
  4. Comparison of Strings
    By Cero.Uno in forum New To Java
    Replies: 3
    Last Post: 02-11-2008, 02:46 AM
  5. Help with drawing strings!
    By JavaInLove in forum AWT / Swing
    Replies: 1
    Last Post: 02-05-2008, 03:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •