JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java SE 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 代換特殊字串有何好方法
hawkhero





發文: 67
積分: 0
於 2008-05-06 13:43 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
hi, 各位:

若有一個文檔如下:
name:$myname$ id:$myid$ email:$myemail$
...

其中以'$'符號包含的字串如$myname$視同變數,要由程式去代換成值,範例結果如下:
name:John id:001 email:john@org.tw
...

請教有什麼適合的class或方法做這樣的事嗎?
感謝!


reply to postreply to post
作者 Re:代換特殊字串有何好方法 [Re:hawkhero]
qrtt1





發文: 1755
積分: 32
於 2008-05-06 19:50 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
http://velocity.apache.org/
http://freemarker.sourceforge.net/


reply to postreply to post
蝸牛角上爭何事?石火光中寄此身,隨富隨貧且歡樂,不開口笑是癡人。
my notes
作者 Re:代換特殊字串有何好方法 [Re:hawkhero]
jason_chan19





發文: 175
積分: 1
於 2008-05-07 11:02 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
利用Groovy的template engines也可以做到,如下:

1
2
3
4
5
6
7
8
<myFile.template>
 
name:${myname} id:${myid} email:${myemail}
 
or
 
name:$myname id:$myid email:$myemail
 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<Main.groovy>
 
import java.io.*;
import groovy.text.*
 
class Main {
 
    static void main(args) {
      def url = Main.class.getResource("./myFile.template");
      
      def file = new File(url.getFile());
      def binding = ['myname': 'John',
                     'myid' : '001',
                     'myemail' : 'john@org.tw'];
    
      def engine = new SimpleTemplateEngine();
      def template = engine.createTemplate(file);
      def writable = template.make(binding);
      println writable;     //可得到 name:John id:001 email:john@org.tw
                   
  }
}
 


可以參考 http://groovy.codehaus.org/Groovy+Templates

至於Groovy的介紹請參考
http://groovy.codehaus.org/

Groovy...

* is an agile and dynamic language for the Java Virtual Machine
* builds upon the strengths of Java but has additional power features inspired by languages like Python, Ruby and Smalltalk
* makes modern programming features available to Java developers with almost-zero learning curve
* supports Domain-Specific Languages and other compact syntax so your code becomes easy to read and maintain
* makes writing shell and build scripts easy with its powerful processing primitives, OO abilities and an Ant DSL
* increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications
* simplifies testing by supporting unit testing and mocking out-of-the-box
* seamlessly integrates with all existing Java objects and libraries
* compiles straight to Java bytecode so you can use it anywhere you can use Java


jason_chan19 edited on 2008-05-07 11:13
reply to postreply to post
你若不想做,會找到一個藉口、你若想做,會找到一個方法
作者 Re:代換特殊字串有何好方法 [Re:hawkhero]
hawkhero





發文: 67
積分: 0
於 2008-05-07 15:00 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
感謝兩位,我會再研究以上資源。

hawkhero edited on 2008-05-07 15:05
reply to postreply to post
» JWorld@TW »  Java SE 討論區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8