Groovy   发布时间:2022-04-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Groovy Tip 34 Groovy语言的here-docs大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_801_0@
@H_801_0@

              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 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>

@H_801_0@

大佬总结

以上是大佬教程为你收集整理的Groovy Tip 34 Groovy语言的here-docs全部内容,希望文章能够帮你解决Groovy Tip 34 Groovy语言的here-docs所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:34docsgroovytip语言
猜你在找的Groovy相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap