4 Replies Latest reply on Apr 29, 2007 8:13 PM by 807599

    Insert a String into a another String @ a specifi point

    807599
      Hi,
      I want to insert a String into another one at the exact point of the 3rd occurence of '>' in the String - is there a handy way to do this?

      For example: String toInsert = "abc";
      String original = "<hello><there><my><friend>";

      and my combined String would look like:

      "<hello><there><my>abc<friend>"

      Thanks,

      C
        • 1. Re: Insert a String into a another String @ a specifi point
          569573
          Use a substring to split it into two, concat them together to form a new String.
          AB -> A and B
          A and B and C -> ABC
          • 2. Re: Insert a String into a another String @ a specifi point
            rahul_akkina
            Hey friend,

            Checkout the below example where we are making use of a util class "StringTokenizer" and separating set of strings and inserting the supposed value accordingly by assuming ">" as a delimter.
            import java.util.StringTokenizer;
            
            
            public class StringEditUtils{
            
              public String make(String original,String toInsert,int pos){
            
                 int i = 0;
                 StringBuffer strbuf = new StringBuffer(); 
            
                 /*as per the condition separating string using a delimter ">" (closed angular brakets) */
                 String delim = ">";  
            
               
                 StringTokenizer strToken = new StringTokenizer(original,delim);
            
            
                 while(strToken.hasMoreElements()){
               
                      strbuf.append(strToken.nextElement());
                      strbuf.append(delim);
                      i = i + 1;
            
                     // appends the string which is supposed to be inserted after specified position
                      if(i == pos) 
                       strbuf.append(toInsert);          
            
                 }
            
            
                 return strbuf.toString();
            
              }
            
            
              public static void main(String args[]){
            
                StringEditUtils util = new StringEditUtils();
            
                String toInsert = "abc"; 
                String original = "<hello><there><my><friend>";
            
               /*inserting "abc" at  3rd position */
                System.out.println( util.make(original,toInsert,3) );
            
            
              }
            
            
            }
            Hope this might help :)

            REGARDS,
            RaHuL
            • 3. Re: Insert a String into a another String @ a specifi point
              800649
                   public static void main(String[] args) {
                        String original = "<hello><there><my><friend>";
                        char gt = '>';
                        String text = "abc";
                        int index = original.indexOf(gt, original.indexOf(gt, original.indexOf(gt)+1)+1)+1;
                        
                        String newString = original.substring(0, index)+text+original.substring(index);
                        
                        System.out.println(newString);
                   }
              surround this code with a try catch block and handle appropriatelly the exceptions

              Hope that helps
              • 4. Re: Insert a String into a another String @ a specifi point
                807599
                String new = original.replaceFirst("(?<=<my>)",toInsert);