Wednesday, November 10, 2010

Code that creates the code to create text

Many times it was required that I need to write a code that writes a particular text.
When such a requirement was being faced repetitively, i decided to write a small snippet of code for it.

@Action
    public void createCode() {
       
        String txt = txtarea.getText();
        String[] s2=txt.split("\\n");
        StringBuilder sb = new StringBuilder("");
        sb.append("public static String createCode()\n");
        sb.append("{\n");
        sb.append("\tStringBuilder sb=new StringBuilder(\"\");\n");
        for(String s:s2)
        {
            sb.append("\tsb.append(\"");
            sb.append(s);
            sb.append("\\n\");\n");
        }
        sb.append("\treturn sb.toString();");
        sb.append("\n}");
        code.setText(sb.toString());
    }

I have a complete application for it as well. But I do not have web-space where I can host it.
And attachments are not allowed in blog. :(

No comments:

Post a Comment