如何在groovy中将一行限制为每行80个字符
我有一个字符串,它包含一些格式化内容,每行后面都有如何在groovy中将一行限制为每行80个字符,groovy,Groovy,我有一个字符串,它包含一些格式化内容,每行后面都有LineFeed。我想格式化该变量的内容,以限制每行不超过80个字符 有人能帮我用Groovy吗 出于测试目的,我将内容复制到一个文件中 String fileContents = new File('E://Projects//temp//license').text println fileContents 文件内容或控制台输出 List of connectivities are: Valid [Metadata Exchange
LineFeed
。我想格式化该变量的内容,以限制每行不超过80个字符
有人能帮我用Groovy吗
出于测试目的,我将内容复制到一个文件中
String fileContents = new File('E://Projects//temp//license').text
println fileContents
文件内容或控制台输出
List of connectivities are:
Valid [Metadata Exchange for Microsoft Visio]
Valid [Metadata Exchange for Microstrategy]
Valid [Metadata Exchange for Microsoft SQL Server Reporting Services and Analysis Services]
Valid [Metadata Exchange for Netezza]
Valid [Metadata Exchange for Oracle]
Valid [Metadata Exchange for Oracle BI Enterprise Edition]
Valid [Metadata Exchange for Oracle Designer]
Command ran successfully
更新
这是我在tim_yates回答后使用的
def es=lic.entrySet()
xml.licInfo() {
int i=0
es.each{
if(!it.key.contains("failed with error"))
{
String val=new String(it.value)
license(name:it.key,value:trimOutput(val),assignedTo:resultRows[i++])
}
}
}
def trimOutput(text)
{
text=text.tokenize( '\n' )*.toList()*.collate(90)*.collect { it.join() }.flatten().join( '\n' )
text
}
但是is给了我以下的例外
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.collate() is applicable for argument types: (java.lang.Integer) values: [90]
Possible solutions: clone(), collect(groovy.lang.Closure), collect(groovy.lang.Closure), clear(), clear(), clear()
更多更新(println es的控制台输出)
这里有两种不同的方法,这取决于您想要对长度超过80个字符的行执行的操作
def text = '''List of connectivities are:
| Valid [Metadata Exchange for Microsoft Visio]
| Valid [Metadata Exchange for Microstrategy]
| Valid [Metadata Exchange for Microsoft SQL Server Reporting Services and Analysis Services]
| Valid [Metadata Exchange for Netezza]
| Valid [Metadata Exchange for Oracle]
| Valid [Metadata Exchange for Oracle BI Enterprise Edition]
| Valid [Metadata Exchange for Oracle Designer]
|
|Command ran successfully'''.stripMargin()
// Strip everything after 80 chars
println text.tokenize( '\n' )*. // Split the lines based on newline character
take( 80 ). // Only take upto the first 80 chars of each String
join( '\n' ) // And join them back together with '\n' between them
// Add newline if line is over 80 chars
println text.tokenize( '\n' )*. // Split the lines based on newline character
toList()*. // Convert each String to a list of chars
collate(80)*. // Split these into multiple lists, 80 chars long
collect { it.join() }. // Join all of the chars back into strings
flatten(). // Flatten the multiple lists of Strings into one
join( '\n' ) // And join these strings back together with '\n' between them
编辑 编辑后,是否可以执行以下操作:
String trimOutput( String input, int width=90 ) {
input.tokenize( '\n' )*.
toList()*.
collate( width )*.
collect { it.join() }.
flatten().
join( '\n' )
}
xml.licInfo {
lic.eachWithIndex { key, value, idx ->
// Shouldn't this be 'value', not 'key'?
if( !key.contains( 'failed with error' ) ) {
license( name: key, assignedTo: idx, trimOutput( value ) )
}
}
}
我认为您需要更改为检查lic映射值中的“failed with error”,而不是您当前使用的密钥,但我不能确定)
编辑2 如果坚持使用groovy 1.8.1,则没有
collate
方法,因此您必须:
List.metaClass.collate={size->
def rslt=delegate.inject([[]){ret,elem->
(ret.last()=大小?ret另一种使用注入的溶液:
String trimOutput( String input, int width=90 ) {
input.tokenize('\n')*.inject('') { output, ch ->
output.size() % (width + 1) ?
(output + ch) :
(output + '\n' + ch)
}.join('\n')
}
或注入和整理的组合:
String trimOutput( String input, int width=90 ) {
input.tokenize('\n').inject([]) { lines, line ->
lines + line.toList().collate(width)*.join()
}.join('\n')
}
你想用超过80个字符做什么?删除它们?还是添加新行?@tim_yates:添加新行,很抱歉反应太晚。你有一个关于lic
和xml
是什么的小例子吗?@tim_yates:xml
是classgroovy.xml.MarkupBuilder
,我正在发布es
的一部分。lic是一个地图。更新d我的答案…使用groovy进行手指交叉编辑看起来非常简单。Thanks@Ricky别担心!很高兴我能帮上忙,玩得开心!:-)我的问题是,text
作为StringBuffer出现。似乎StringBuffer
没有tokenize
&我的text
是StringBuffer,我尝试了text.toString
,但没有luck@Rickygroovy的哪个版本?assert new StringBuffer('this a test')。tokenize()=['this','is','a','test']
在1.8.6中运行良好:-/@Ricky断言行通过了吗?如果没有,您能否编辑您的问题,使其显示您面临的实际情况、您正在尝试的代码以及引发的异常?它运行良好,只有一个小问题,即使在语句后遇到新行,它也会插入另一个新行,因此需要2行换行s是there@Ricky尝试将`output.size()%(width+1)``更改为`(output.size()+1)%(width+1)``我做了相应的更改,但两个句子之间仍然存在行距(2个LF,第一个语句位于下一行,第二个是空白新行)
String trimOutput( String input, int width=90 ) {
input.tokenize('\n')*.inject('') { output, ch ->
output.size() % (width + 1) ?
(output + ch) :
(output + '\n' + ch)
}.join('\n')
}
String trimOutput( String input, int width=90 ) {
input.tokenize('\n').inject([]) { lines, line ->
lines + line.toList().collate(width)*.join()
}.join('\n')
}