Results 1 to 5 of 5
  1. #1
    ariz is offline Member
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default manipulating a string (Urgent)

    Hi,
    Please help me manipulate the following string, I am really stuck.

    str= ~valveopen leak okh2 leak valveopen okh1;


    in this string I want to delete those substrings that have a pair. Forexample
    ~valveopen and valveopen is a pair
    leak and ~leak is a pair
    and i want to retain those substrings that donot have pairs.so after making changes the string should be like this
    str=okh2 okh1
    I would really appreciate any kind of help.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    (Urgent)
    You would be wise not to state this in your posts. Even if it is urgent to you, realize that it is not urgent to us. Many here take offense to this as it implies to them that a) the poster thinks that his post is more important than everyone else's, and b) that the poster wants to put pressure on the volunteers who come here to help on their own free time. Just a friendly word of advice.

    Also, please post valid Java code. What you've posted are not Strings making it harder to figure out exactly what you're trying to do. You should use quotes properly for instance. Programming is an exercise in precision and the compiler is not very forgiving. You should thus be as precise as possible in your questions here. Also, what does the squiggle, "~" represent?

    Good luck.
    Last edited by Fubarable; 03-31-2009 at 04:03 AM.

  3. #3
    ariz is offline Member
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default

    Sorry no use of urgent again.Thankyou for bringing this to my knowledge.

    str="~valveopen leak okh2 ~leak valveopen okh1";
    think of "~" as a negation sign....forexample "~leak" means "no leak" and "~valveopen" means "valve not open". In contrast only "leak" means that there is a leak and "valveopen" means valve is open. So basically the contradicting parts have to be removed from str. the resulting str is
    str="okh1 okh2";
    thanks for ur interest.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    So will every pair have a String and its negation?

    If so, there probably some regex that will solve this that is neat, efficient, and beyond my current abilities. For me, I'd probably split the String via String#split(" ") and then check iterate through the array looking for matches.

  5. #5
    ariz is offline Member
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default

    ok I did that see my code
    String regex=" ";
    String [] cancel=newsent.split(regex); //newsent is the string that we are manipulating
    if(cancel.length != 1){
    for(int t=0; t<cancel.length; t++){
    String tt=cancel[t];
    if(tt.charAt(0)=='~')
    g =true;
    else
    g=false;
    for(int y=0;y<cancel.length;y++){
    String uu=cancel[y];
    if(g==true){
    if(tt.substring(1).equalsIgnoreCase(uu)){
    for(int w=0; w<cancel.length; w++){
    if(cancel[w].equalsIgnoreCase(tt) || cancel[w].equalsIgnoreCase(uu)){
    continue;
    }
    finl+=" "+cancel[w];
    finl=finl.trim();


    }
    cancel=finl.split(regex);
    t=-1;
    break;
    }
    }
    else {
    if(uu.substring(1).equalsIgnoreCase(tt)){
    for(int w=0; w<cancel.length; w++){
    if(cancel[w].equalsIgnoreCase(tt) || cancel[w].equalsIgnoreCase(uu)){
    continue;
    }
    finl+=" "+cancel[w];
    finl=finl.trim();
    }
    cancel=finl.split(regex);
    t=-1;
    break;

    }
    }
    }
    }


    }
    but there is some problem with updation of cancel array..I dont know how to keep it update to the current state of newsent as it is also used in the outermost for loop.Please use the str="~valveopen leak okh2 ~leak valveopen okh1"; as a test case and try to reslove the errors.

Similar Threads

  1. Manipulating XML
    By JosephMConcepcion in forum XML
    Replies: 2
    Last Post: 04-26-2009, 12:01 AM
  2. Replies: 14
    Last Post: 04-10-2009, 08:46 AM
  3. combine string[] into string like perl's join function
    By tekberg in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2009, 01:05 PM
  4. Replies: 5
    Last Post: 01-11-2009, 10:12 AM
  5. Replies: 0
    Last Post: 11-20-2007, 04:59 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
  •