8 Replies Latest reply on Jan 15, 2007 9:16 PM by 807607

    Quickest way to substitute values in a String...

    807607
      I have a string that represents the contents of a html file. I want to substitute all the custom variables (shown as {value} ) in the String with a new value. I am using the Java REGEXP to find the start/end position of all the custom variables that need to be substituted. The start/end position and new value are placed in a Frag object. After all the REGEXP matches have been found i loop through the Frag objects and place the new value into the String (called template in code below)
      String leftSplit = template.substring(0, frag.getStartPos());
      String rightSplit = template.substring(frag.getEndPos(), template.length());
      String template = new StringBuffer(leftSplit).append(frag).append(rightSplit).toString();
      My query is on the above 3 lines of code. I probably havent done it in the most efficient manner possible. Any suggestions? I expect to substitute about 20 values per file and there could be maybe 100 files that need processing.
        • 1. Re: Quickest way to substitute values in a String...
          807607
          I had a problem similar. For this i created a metod that generate the html code in the String using the variables.

          For example:
          public class HTMLCreator
          {
                private String name;
                private String html;
                
                public HTMLCreator()
                {
                     name = " ";
                }
          
                public void HTMLCreate()
                {
                      html = "<head>" +
                                   "<meta http-equiv=\"Content-Type\" content=\"text/html;  charset=iso-8859-1\" /> " +
                                   "<title> Hello World HTML</title>" +
                                   "</head>" +
                                   "<body>" +
                                   "Hello World: <strong>" + name +
                                   "</strong>" +
                                  "</body>" +
                                  "</html>";
                  
                          // You can create the .html file here too
                 }
          
                public String getName()
                {
                     return name;
                }
          
                public void setName(String name)
                {
                      this.name = name;
                }
          }
          Thats it!
          Davi Romero
          • 2. Re: Quickest way to substitute values in a String...
            791266
            Hi,

            Should all tags be replaced with the same value?

            Kaj
            • 3. Re: Quickest way to substitute values in a String...
              800774
              Why not use String.replaceAll?
              • 4. Re: Quickest way to substitute values in a String...
                807607
                Many thanks for your responses. The variable names are all different and the values they are replaced with are all different. Therefore a replace all will not work.
                • 5. Re: Quickest way to substitute values in a String...
                  791266
                  You can probably take a look at this thread:
                  http://forum.java.sun.com/thread.jspa?forumID=31&messageID=2687247&threadID=549957

                  I have posted code to a similar problem in that thread.

                  Kaj
                  • 6. Re: Quickest way to substitute values in a String...
                    807607
                    Yes, that's very inefficient. You can use a Rewriter to do all the replacements in one pass:

                    http://elliotth.blogspot.com/2004/07/java-implementation-of-rubys-gsub.html
                    • 7. Re: Quickest way to substitute values in a String...
                      807607
                      nice link, useful little tool

                      do you by chance have a link to the code sniplet which does variable substitution in an elegant way?

                      i saw it in one blog and never bookmarked it :P
                      • 8. Re: Quickest way to substitute values in a String...
                        807607
                        do you by chance have a link to the code sniplet
                        which does variable substitution in an elegant way?
                        I don't know what snippet you're talking about, but here's a quick-and-dirty example that shows how a Rewriter can be used for a job like this.
                        import java.util.*;
                        
                        public class Merger extends Rewriter
                        {
                          Map<String, String> varTable;
                          
                          public Merger()
                          {
                            super("\\{(\\w+)\\}");
                            
                            varTable = new HashMap<String, String>();
                            varTable.put("name", "Robert Parr");
                            varTable.put("age", "42");
                            varTable.put("city", "Municiberg");
                            varTable.put("alias", "Mr. Incredible");
                          }
                        
                          public String replacement()
                          {
                            return varTable.get(group(1));
                          }
                        }