読者です 読者をやめる 読者になる 読者になる

Groovyで文字列を行に分ける

groovy

行単位に分け分解して、行ごとに繰り返しの処理を行う。

ソース

Groovy_in_Action_english =
"""Groovy, the brand-new language for the Java platform, brings to Java many of the features that have made Ruby popular. Groovy in Action is a comprehensive guide to Groovy programming, introducing Java developers to the new dynamic features that Groovy provides. To bring you Groovy in Action, Manning again went to the source by working with a team of expert authors including both members and the Manager of the Groovy Project team. The result is the true definitive guide to the new Groovy language.

Groovy in Action introduces Groovy by example, presenting lots of reusable code while explaining the underlying concepts. Java developers new to Groovy find a smooth transition into the dynamic programming world. Groovy experts gain a solid reference that challenges them to explore Groovy deeply and creatively.

Because Groovy is so new, most readers will be learning it from scratch. Groovy in Action quickly moves through the Groovy basics, including:

 * Simple and collective Groovy data types
 * Working with Closures and Groovy Control Structures
 * Dynamic Object Orientation, Groovy style

Readers are presented with rich and detailed examples illustrating Groovy's enhancements to Java, including

 * How to Work with Builders and the GDK
 * Database programming with Groovy

Groovy in Action then demonstrates how to Integrate Groovy with XML, and provides,

 * Tips and Tricks
 * Unit Testing and Build Support
 * Groovy on Windows
 
An additional bonus is a chapter dedicated to Grails, the Groovy Web Application Framework. """


Groovy_in_Action_english.split(/\n/).each {line->
    println "\"" + line + "\""
}

println "\n行頭、行末の空白類を取り除く"
Groovy_in_Action_english.split(/\n/).each {line->
    println "\"" + line.trim() + "\""
}

println "\n\nGで始まる行"
Groovy_in_Action_english.split(/\n/).each {line->
    (line =~/^G.*/).each{ 
        println "\""+ it +"\""
    }
}

結果

[D:\workspace\groovy_SandBox]groovy Q046_文字列を行に分ける.groovy
[D:\workspace\groovy_SandBox]groovy Q046_文字列を行に分ける.groovy
"Groovy, the brand-new language for the Java platform, brings to Java many of the features that have made Ruby popular. Groovy in Action is a comprehensive guide to Groovy programming, introducing Java developers to the new dynamic features that Groovy provides. To bring you Groovy in Action, Manning again went to the
source by working with a team of expert authors including both members and the Manager of the Groovy Project team. The result is the true definitive guide to the new Groovy language."
""
"Groovy in Action introduces Groovy by example, presenting lots of reusable code while explaining the underlying concepts. Java developers new to Groovy find a
smooth transition into the dynamic programming world. Groovy experts gain a solid reference that challenges them to explore Groovy deeply and creatively."
""
"Because Groovy is so new, most readers will be learning it from scratch. Groovy in Action quickly moves through the Groovy basics, including:"
""
" * Simple and collective Groovy data types"
" * Working with Closures and Groovy Control Structures"
" * Dynamic Object Orientation, Groovy style"
""
"Readers are presented with rich and detailed examples illustrating Groovy's enhancements to Java, including"
""
" * How to Work with Builders and the GDK"
" * Database programming with Groovy"
""
"Groovy in Action then demonstrates how to Integrate Groovy with XML, and provides,"
""
" * Tips and Tricks"
" * Unit Testing and Build Support"
" * Groovy on Windows"
" "
"An additional bonus is a chapter dedicated to Grails, the Groovy Web Application Framework. "

行頭、行末の空白類を取り除く
"Groovy, the brand-new language for the Java platform, brings to Java many of the features that have made Ruby popular. Groovy in Action is a comprehensive guide to Groovy programming, introducing Java developers to the new dynamic features that Groovy provides. To bring you Groovy in Action, Manning again went to the
source by working with a team of expert authors including both members and the Manager of the Groovy Project team. The result is the true definitive guide to the new Groovy language."
""
"Groovy in Action introduces Groovy by example, presenting lots of reusable code while explaining the underlying concepts. Java developers new to Groovy find a
smooth transition into the dynamic programming world. Groovy experts gain a solid reference that challenges them to explore Groovy deeply and creatively."
""
"Because Groovy is so new, most readers will be learning it from scratch. Groovy in Action quickly moves through the Groovy basics, including:"
""
"* Simple and collective Groovy data types"
"* Working with Closures and Groovy Control Structures"
"* Dynamic Object Orientation, Groovy style"
""
"Readers are presented with rich and detailed examples illustrating Groovy's enhancements to Java, including"
""
"* How to Work with Builders and the GDK"
"* Database programming with Groovy"
""
"Groovy in Action then demonstrates how to Integrate Groovy with XML, and provides,"
""
"* Tips and Tricks"
"* Unit Testing and Build Support"
"* Groovy on Windows"
""
"An additional bonus is a chapter dedicated to Grails, the Groovy Web Application Framework."


Gで始まる行
"Groovy, the brand-new language for the Java platform, brings to Java many of the features that have made Ruby popular. Groovy in Action is a comprehensive guide to Groovy programming, introducing Java developers to the new dynamic features that Groovy provides. To bring you Groovy in Action, Manning again went to the
source by working with a team of expert authors including both members and the Manager of the Groovy Project team. The result is the true definitive guide to the new Groovy language."
"Groovy in Action introduces Groovy by example, presenting lots of reusable code while explaining the underlying concepts. Java developers new to Groovy find a
smooth transition into the dynamic programming world. Groovy experts gain a solid reference that challenges them to explore Groovy deeply and creatively."
"Groovy in Action then demonstrates how to Integrate Groovy with XML, and provides,"

説明

行ごとに対しての処理は、eachで行える。


空白類文字を取り除くメソッドの違い

Groovy Ruby
trim strip

実行環境














Groovyの詳細についてはJavadocと以下の書籍を参考にしている。

Groovyイン・アクション
Dierk Konig Andrew Glover Paul King Guillaume Laforge Jon Skeet
毎日コミュニケーションズ
売り上げランキング: 294340


問題自体は以下の書籍のもの。rubyと似てる部分も多いので、ヒントにもなる。
写経でもいいが自分で考えるために他言語の例をGroovyで置き換えてる。

Rubyレシピブック 第2版 268の技
青木 峰郎 後藤 裕蔵 高橋 征義
ソフトバンク クリエイティブ
売り上げランキング: 80467


Groovyイン・アクションを読むならあった方が便利かな。

ブックストッパー

トモエ算盤
売り上げランキング: 614


Rubyレシピブックは「ほんたった」で立ててる

EDISON ほんたった黒(ハードケース入り)
株式会社エジソン
売り上げランキング: 2164


広告を非表示にする