gpt4 book ai didi

groovy - 如何在groovy中将一行限制为每行80个字符

转载 作者:行者123 更新时间:2023-12-04 05:49:04 24 4
gpt4 key购买 nike

我有一个字符串,其中包含一些带有 LineFeed 的格式化内容每行之后。我想格式化该变量的内容以限制每行不超过 80 个字符。

有人可以在 Groovy 中帮助我吗?

出于测试目的,我将内容复制到文件中

String fileContents = new File('E://Projects//temp//license').text
println fileContents

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
}

但给了我以下异常(exception)
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 的控制台输出)
[license_all =Edition:          BAAC Standard
Software Version: 6.5
Distributed by: ABC
Issued on: 2012-Feb-06
Validity period: Non-Expiry
Serial number: 210502
Deployment level: Production

List of supported platforms are:
[All operating systems] is authorized for [100] logical CPUs
Number of authorized repository instances: 100
Number of authorized CAL usage count: 100

List of connectivities are:

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]
Valid [Metadata Exchange for Oracle Warehouse Builder]
Valid [Metadata Exchange for Popkin System Architect]
Valid [Metadata Exchange for SAP R/3]
Valid [Metadata Exchange for Select SE]
Valid [Metadata Exchange for Silverrun - RDM]
Valid [Metadata Exchange for SQL Server]
Valid [Metadata Exchange for Sybase ASE]
Valid [Metadata Exchange for Sybase PowerDesigner]
Valid [Metadata Exchange for Teradata]

Command ran successfully.
]

最佳答案

这是两种不同的方法,具体取决于您要对长度超过 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 映射值中的“因错误而失败”,而不是您目前拥有的 key ,但我无法确定)

编辑2
如果您坚持使用 groovy 1.8.1,则没有 collate方法,所以你必须 roll your own :
List.metaClass.collate = { size ->
def rslt = delegate.inject( [ [] ] ) { ret, elem ->
( ret.last() << elem ).size() >= size ? ret << [] : ret
}
!rslt.last() ? rslt[ 0..-2 ] : rslt
}

关于groovy - 如何在groovy中将一行限制为每行80个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10311567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com