我是靠谱客的博主 超帅帅哥,这篇文章主要介绍groovy linux 换行符,尝试拆分换行符时,Groovy返回错误,现在分享给大家,希望可以做个参考。

tim_yates..

7

看起来像message.payload返回一个byte[]你需要回到一个字符串:

def lines = new String( message.payload, 'UTF-8' ).split( 'n' )

应该得到它:-)

另外,我更喜欢写这样的东西:

def mesType = new String( message.payload, 'US-ASCII' ).split( 'n' ).take( 2 ).with { lines ->

switch( lines ) {

case { it.any { line -> line.startsWith( '123456' ) } } : 'MES1' ; break

case { it.any { line -> line.startsWith( '654321' ) } } : 'MES2' ; break

case { it.any { line -> line.startsWith( '234561' ) } } : 'MES3' ; break

default :

''

}

}

而不是大量if...else具有远程字符串访问权限的块(即:如果您获得的行只有3个字符,或者有效负载中只有1行,则会失败)

使用Groovy 1.5.6,你会遇到:

def mesType = new String( message.payload, 'US-ASCII' ).split( 'n' )[ 0..1 ].with { lines ->

并保持你的手指交叉它有效载荷至少有2行

或者您将需要引入一种方法来从数组中获取最多2个元素

你能试一下吗:

这可能是with1.5.6中的突破(不确定)...尝试将其展开回原来的状态:

def lines = new String( message.payload, 'US-ASCII' ).split( 'n' )[ 0..1 ]

def mesType = 'empty'

if( lines.any { line -> line.startsWith( '123456' ) } ) mesType = 'MES1'

else if( lines.any { line -> line.startsWith( '654321' ) } ) mesType = 'MES2'

else if( lines.any { line -> line.startsWith( '234561' ) } ) mesType = 'MES3'

最后

以上就是超帅帅哥最近收集整理的关于groovy linux 换行符,尝试拆分换行符时,Groovy返回错误的全部内容,更多相关groovy内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部