5 Replies Latest reply: May 17, 2007 8:46 AM by 807606 RSS

    need some help with the string

    807606
      hi guys if you have some time i need your advise

      i have a string
       "Today+is+raining"
      and i need to get strings:
       "Today+is+raining"
       "Today+is"
       "Today"
      the codes i have are:
      String todayisraining = "Today+is+raining";
             out.println(todayisraining); // "Today+is+raining"
             
             String split1[] = todayisraining.split("\\+");
             String raining = "";
             for(int i=0; i<split1.length;i++) { raining = split1; }
      String todayisplus = todayisraining.substring(0,todayisraining.length() - raining.length());
      String todayis = todayisplus.substring(0,todayisplus.length()-1);
      out.println(todayis); // "Today+is"

      String split2[] = todayis.split("\\+");
      String is = "";
      for(int i=0; i<split2.length;i++) { is = split2[i]; }
      String todayplus = todayis.substring(0,todayis.length() - is.length());
      String today = todayplus.substring(0,todayplus.length()-1);
      out.println(today); // "Today"
      bad solution because posted by hand :(
      String todayisraining = "Today+is+raining";
             String split[] = todayisraining.split("\\+");
              for(int n=0; n<split.length;n++){
              for(int m=0; m<=split.length-n-1;m++) {   
              out.println( split[m]);// "Today" "is" "raining" , "is" "raining" , "raining"
              }
              }  
      also bad solution, because string consist from separate objects
      should i use recursive loop or something? thank you
        • 1. Re: need some help with the string
          807606
          Hi,
          Try using substring and indexOf on the plus signs, that should do it.
          Good luck,
          Jezzica85
          • 2. Re: need some help with the string
            807606
            thank you Jezzica85 for quick responce it's working but one more and i hope last question, now i have a simple code:
            import java.io.*;
            
            public class strings { 
                
                public static void main(String args[]) throws IOException {
                    
                    String string1 = "Today+is+raining+outside";
                    
                    int i = string1.lastIndexOf("+");
                    String string2 = string1.substring(0,i);
                    
                    int j = string2.lastIndexOf("+",i);
                    String string3 = string2.substring(0,j);
                    
                    System.out.println(string1 + string2 + string3); 
                }
            }
            in the outut i get
            "Today+is+raining+outside"
            "Today+is+raining"
            "Today+is"
            and last should be
            "Today"
            maybe you know how to execute this program automatically using loop or something to get all those strings, thanks a lot
            • 3. Re: need some help with the string
              807606
              this will do it make sure u know what is happening so that you can understand it, u should debug the code to see what is happening
                String string1 = "Today+is+raining+outside";
                          String result=null;
                          int start=0;
                          int finish=string1.length();
                          while(finish>=0){
                                result= string1.substring(start,finish);
                                finish=result.length()-(result.length()-result.lastIndexOf("+"));
                                 System.out.println(result);
                           }
              basically instead of using the intial string1 each time to get the substring i stored each new substring in result therefore all i needed to do was check for the last index of "+" each time
              • 4. Re: need some help with the string
                807606
                now i have code like this, but it doesn't work:
                import java.io.*;
                import java.util.*;
                import java.text.*;
                
                public class strings { 
                    
                  strings(){String n;}
                    
                    public static void main(String args[]) throws IOException {
                        
                        strings st = new strings();
                        String string3 = st.method();
                         System.out.println(string3); 
                        String sp[]= string3.split("\\+");
                        String check = "";
                        for(int i=0;i<sp.length;i++){check=sp;}
                String n = string3.substring(0,string3.length() - check.length());
                if(n.endsWith("+")){
                new naujas(n); //cannot find simbol constructor
                }
                }

                public String method(){

                String string1 = "Today+is+raining+outside";

                int i = string1.lastIndexOf("+");
                String string2 = string1.substring(0,i);

                return string2;
                }
                }
                i need the same thing - to get output:
                "Today+is+raining+outside"
                "Today+is+raining"
                "Today+is"
                "Today"
                Thank you for your help
                • 5. Re: need some help with the string
                  807606
                  i didnt notice your post bouncer i'll check your code, thank you very much for your time