Grails:分割包含管道的字符串 [英] Grails: Splitting a string that contains a pipe

查看:108
本文介绍了Grails:分割包含管道的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图分割字符串。简单的例子工作:

  groovy:000>打印abc,def.split(,); 
[abc,def] ===> null
groovy:000>

但是,我不需要逗号,而需要将其拆分为管道,而且我没有想要的结果:

  groovy:000>打印abc | def.split(|); 
[,a,b,c,|,d,e,f] ===> null
groovy:000>

当然,我的第一选择是从管道切换( | )以逗号()作为分隔符。



但现在我很好奇:为什么这不起作用?转义管道( \ | )似乎没有帮助:

 常规:000>打印abc | def.split(\ |); 
ERROR org.codehaus.groovy.control.MultipleCompilationErrorsException:启动失败,groovysh_parse:1:意外的char:'\'@第1行,第24列。
printabcdef.split(\ |);
^

1错误
|
at java_lang_Runnable $ run.call(Unknown Source)
groovy:000>


解决方案

您需要拆分 \\ |


I’m trying to split a String. Simple examples work:

groovy:000> print "abc,def".split(",");
[abc, def]===> null
groovy:000>

But instead of a comma, I need to split it on pipes, and I’m not getting the desired result:

groovy:000> print "abc|def".split("|");
[, a, b, c, |, d, e, f]===> null
groovy:000>

So of course my first choice would be to switch from pipes (|) to commas (,) as delimiters.

But now I’m intrigued: Why is this not working? Escaping the pipe (\|) doesn't seem to help:

groovy:000> print "abc|def".split("\|");
ERROR org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, groovysh_parse: 1: unexpected char: '\' @ line 1, column 24.
   print "abcdef".split("\|");
                          ^

1 error
|
        at java_lang_Runnable$run.call (Unknown Source)
groovy:000>

解决方案

You need to split on \\|.

这篇关于Grails:分割包含管道的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆