2012-01-09 68 views

回答

5
String str = "I will not stand for this antidisestablishmentarianism!" 

def newStr = (str =~ /(\w{10})/).replaceAll("\$1\n") 

println newStr 

的10确定单词的长度被分裂

+2

的字符串也可使用分裂['字符串#的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

1

除了聂的回答,您可能感兴趣的替代解决方案。

我不知道你的确切任务是什么,但有时连字可以更自然。


检查出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; 

用例:http://www.webdesignerwall.com/demo/word-wrap/

+0

感谢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

相关问题