我想将在字符串中找到的大字(如10个字符或更多)分解为多行。例如,我想转换如何使用Groovy正则表达式打破字符串中的大字词
我不会支持这种反现实主义!
到
我不会站在这个antidisest
ablishment
阿里乌主义!
我想将在字符串中找到的大字(如10个字符或更多)分解为多行。例如,我想转换如何使用Groovy正则表达式打破字符串中的大字词
我不会支持这种反现实主义!
到
我不会站在这个antidisest
ablishment
阿里乌主义!
String str = "I will not stand for this antidisestablishmentarianism!"
def newStr = (str =~ /(\w{10})/).replaceAll("\$1\n")
println newStr
的10确定单词的长度被分裂
除了聂的回答,您可能感兴趣的替代解决方案。
我不知道你的确切任务是什么,但有时连字可以更自然。
检查出Hyphenator,用于客户端连字符的Javascript库。
用例:http://hyphenator.googlecode.com/svn/tags/Version%204.0.0/WorkingExample.html
还检查了连字符CSS3属性:http://www.w3.org/TR/css3-text/#hyphenation
还要考虑CSS3 overflow-wrap
财产,迫使词语的中间突破,如果有是没有选择的:
overflow-wrap: break-word;
传统替代名称:
word-wrap: break-word;
感谢Nik和其他人。赞美歌http://code.christophervigliotti.com/2012/01/how-nik-broke-up-large-words-found-in-a-string-using-regex/ – 2012-01-10 16:50:42
的字符串也可使用分裂['字符串#的replaceAll(模式,字符串)'](HTTP ://groovy.codehaus.org/groovy-jdk/java/lang/String.html#replaceAll%28java.util.regex.Pattern,%20java.lang.String%29),比如'str.replaceAll(/(\ w {10})/,“\ $ 1 \ n”)'。 – epidemian 2012-01-10 01:19:36