Groovy Tip 34 Groovy语言的here-docs

              Groovy Tip 34 Groovy语言的here-docs

 

 

 

Groovy语言对Java字符串的改变,除了String对象的很多新功能,最大的好处就是引入了Gstring的概念,这些都在前面的文字中有过详细的阐述。除此之外,Groovy语言还引入了here-docs的概念,这就是本篇将要谈到的。

简单来说,here-docs就是用来解决字符串换行的问题。

在Java语言中,字符串换行有两种解决方法。

一种是通过"+"号连接,这种本身对字符串没有换行,因为字符串本身太长,通过换行来方便我们的阅读。这种场合最常用的是sql语句。如下:

    private static final String sql = "select t.qdbid as itemdbid, "+

                                       "t.itemtype as itemlev, "+

                                       "t.itemname as itemname, "+

                                       "t.sourceid as parentid "+

                                       "from t_sp_qcddvbase t "+

                                       "where t.yearmonth=? and "+

                                       "t.isview LIKE '%Y%' "+ 

                                       "order by t.itemtype, t.qdbid";

 

 

我们可能都写过了类似于上面的sql语句,在上面的语句中,为了方便我们的阅读,我们不得不为了一句sql语句,输入多个"+"号,还有多对引号。这大大的降低了我们的编码效率。同时,由于每一行的字符串都需要引号和"+"号连接,所以阅读性也并不是太好。

在Groovy语言中,我们引入了here-docs的概念,所谓"here-docs",在Groovy语言中,就是由三对双引号引起来的,可以换行的字符串。例如,使用"here-docs",我们就可以把上面的sql语句写成下面的样子:

    private static final String sql = """select t.qdbid as itemdbid,

                                     t.itemtype as itemlev,

                                     t.itemname as itemname,

                                     t.sourceid as parentid 

                                     from t_sp_qcddvbase t

                                     where t.yearmonth=? and

                                     t.isview LIKE '%Y%'  

                                    order by t.itemtype, t.qdbid""";

   

 

可以看到,使用"here-docs"的阅读性就比上面的Java语言的方式要强得多。而且编码也方便了很多。

同时,"here-docs"中的换行也是字符串中的真正换行。曾经,我们在Java语言中要用到字符串的换行,就必须借助于"/n"字符串。比如,一个网上书店的通知单的编码可能是如下的样子:

 

 

         String name = "Wallace";

        

         String sex = "male";

        

         String[] books = new String[]{"Groovy in action","Grails in action"};

       

         String time = "2009-05-20";

        

         StringBuffer sb = new StringBuffer();

        

         sb.append("Dear ");

         if("male".equalsIgnoreCase(sex))

         {

             sb.append("Mr.");

         }

         else if("female".equalsIgnoreCase(sex))

         {

             sb.append("Ms.");

         }

         sb.append(name).append(",/n  ");

        

         sb.append("You has bought ");

        

         sb.append(books.length).append(" books: ");

        

         for(int i=0;i<books.length;i++)

         {

             sb.append(books[i]).append(",");

         }

        

         sb.append("  You will receive it at ").append(time);

         sb.append("/n");

         sb.append("  Thank You!");

        

         System.out.println(sb.toString());

   

 

运行结果为:

Dear Mr.Wallace,

  You has bought 2 books: Groovy in action,Grails in action,  You will receive it at 2009-05-20

  Thank You!

 

可以看到,编码十分的繁琐,代码的阅读性也很差。如果使用"here-docs"的话,编码将会是如下的样子:

 

         String name = "Wallace"

        

         String sex = "male"

        

         String[] books = ["Groovy in action","Grails in action"]

       

         String time = "2009-05-20"

        

         def mrms = sex=='male'?'Mr':'Ms'

               

         def bookNames = books.join(',')

        

         def letter =

"""Dear ${mrms}.${name},

  You has bought ${books.size()} books: ${bookNames},  You will receive it at $time

  Thank You!"""

                      

        println letter

   

 

运行结果为:

Dear Mr.Wallace,

  You has bought 2 books: Groovy in action,Grails in action,  You will receive it at 2009-05-20

  Thank You!

 

 

可以看到,由于"here-docs"也支持Gstring,所以使用"here-docs"对多行字符串进行编码,无论是代码量上,还是可读性上,都强了很多。

最后,值得注意的是,由于"here-docs"也是字符串,同样可以进行字符串的各种操作,请看如下的一个生成html标签的代码:

 

      def list = ['Groovy in action','Grails in action']

     

      def name = 'select1'

     

      def select =

"""<select name="${name}">

"""

  

      StringBuffer sb = new StringBuffer()

 

       list.each{

           sb <<"  <option>${it}</option>/n"

       }

 

      select = select+sb.toString()

     

      select = select+

"""

</select>"""

 

  println select

 

 

里面既有"here-doc",又有双引号的字符串加上Gstring,然后再把它们连接起来,整个编码显得十分的灵活。

运行结果为:

<select name="select1">

  <option>Groovy in action</option>

  <option>Grails in action</option>

 

</select>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值