This discussion is archived
11 Replies Latest reply: Sep 26, 2007 11:50 PM by 807605 RSS

remove some portion of the String

807605 Newbie
Currently Being Moderated
Hi,
My problem is String contains data is

artist1
title1
-1
artist2
title2
2
artist3
title3
-1
artist4
title4
2

i want to remove some portion of the string like artist2, title2, 2 so my updated string should be like

artist1
title1
-1
artist3
title3
-1
artist4
title4
2

Here i am using
String.replaceAll("artist2","");
String.replaceAll("title3","");
String.replaceAll("2","")("[\code]

its deleting whole data in a string, can any one please help me how to delete particular part of the string. Here artist and title are unique and no are reapeated.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  • 1. Re: remove some portion of the String
    796440 Guru
    Currently Being Moderated
    Show your exact code, the exact string you're starting with, the exact string you want to end with, and the exact string you're ending up with instead of what you want. Also clearly describe the rules for determining what is to be deleted.

    When you post code, but it between [code] and [/code]
  • 2. Re: remove some portion of the String
    807605 Newbie
    Currently Being Moderated
    You don't want to use replaceAll in this situation, methinks. You want to use replace( ) and I think that you want to give the replace method the whole string to delete all at once. Also, please note that replace (and replaceAll) don't do anything to the original class that calls the method, the String. You have to set the String = to the result of the method call.
  • 3. Re: remove some portion of the String
    807605 Newbie
    Currently Being Moderated
    here is my code
    public class test
    {
    public static void main(String args[])
    {
    String s = "aritst1"+"\n"+"title1"+"\n"+"1"+"\n"+"artist2"+"\n"+"title2"+"\n"+"1";
    
    System.out.println(s);
    
    s.replaceFirst("artist1"+"\n"+"title1"+"\n"+"1","");
    
    System.out.println(s);
    }
    }
    Here its not replacing. Please help me how can i achieve this with an example.
  • 4. Re: remove some portion of the String
    jwenting Journeyer
    Currently Being Moderated
    you can't "remove some portion of the String" because Strings are immutable.
    What you can do is create a new String which contains everything that was in the old one except the part you don't want.
  • 5. Re: remove some portion of the String
    807605 Newbie
    Currently Being Moderated
    so, what is the use of replaceAll() method? Is it won't modify existing string with new String
  • 6. Re: remove some portion of the String
    807605 Newbie
    Currently Being Moderated
    replaceAll returns a new String which is a modified version of the original. May be more complex than what you really need but check out regular expressions if you're willing to devote some time to a more elegant solution.
  • 7. Re: remove some portion of the String
    807605 Newbie
    Currently Being Moderated
    Hey I had the same problem like you yesterday.

    I tried using regEx with positiv lookahead but noticed that there was a problem when the String went over multiple lines. There is probably some solution to it that i havent found yet.

    So what I did was get the Index of the first text I wanted deleted(in your case I guess "artist2"). Then got the Index of the end of the deletion point(In your case the "2"?) and then formed a new String with the substring from the orginal one and the indexes of the two deletion points.

    Sounds kinda dirty but it did work out. Anyway I'll dig myself into regular expressions more today because if you get them to work, they're a charm :)
  • 8. Re: remove some portion of the String
    807605 Newbie
    Currently Being Moderated
    I tried using regEx with positiv lookahead but noticed that there was a
    problem when the String went over multiple lines. There is probably
    Check out java.util.regex.Pattern.MULTILINE.
  • 9. Re: remove some portion of the String
    807605 Newbie
    Currently Being Moderated
    There's a typo...

    In the first string you have: ar it st1 in the second one ar ti st,

    fix that and it will work,

    Just assign the product od s.replace to s or some other, nes String object

    HTH

    Edited by: G.W. on Sep 27, 2007 7:49 AM
  • 10. Re: remove some portion of the String
    807605 Newbie
    Currently Being Moderated
    hwaite wrote:
    I tried using regEx with positiv lookahead but noticed that there was a
    problem when the String went over multiple lines. There is probably
    Check out java.util.regex.Pattern.MULTILINE.
    The DOTALL flag would be my suggestion.
  • 11. Re: remove some portion of the String
    807605 Newbie
    Currently Being Moderated
    Yeah DOTALL does the trick.

    An with
     String  replaceFirst(String regex, String replacement) 
    I am able to do the rest.

    Thanks guys!