[英]Split function doesn't work
Here is a function which take a long String and return a string divided in paragraph. 这是一个函数,它接受一个长字符串并返回一个分段的字符串。
The problem is that k is empty. 问题是k是空的。 Why
split()
function doesn't work? 为什么
split()
函数不起作用?
private String ConvertSentenceToParaGraph(String sen) {
String nS = "";
String k[] = sen.split(".");
for (int i = 0; i < k.length - 1; i++) {
nS = nS + k[i] + ".";
Double ran = Math.floor((Math.random() * 2) + 4);
if (i > 0 && i % ran == 0) {
nS = nS + "\n\n";
}
}
return nS;
}
String.split(String regex)
takes a regular expression. String.split(String regex)
采用正则表达式。 A dot .
一个点
.
means 'every character'. 意思是'每个角色'。 You must escape it
\\\\.
你必须逃避它
\\\\.
if you want to split on the dot character. 如果你想拆分点字符。
split
expects a regular expression, and "."
split
需要一个正则表达式,并且"."
is a regular expression for "any character". 是“任何字符”的正则表达式。 If you want to split on each
.
如果你想分开每个
.
character, you need to escape it: 性格,你需要逃脱它:
String k[] = sen.split("\\.");
split()
method takes a regex. split()
方法采用正则表达式。 And .
并且
.
is a meta-character, which matches any character except newline. 是一个元字符,它匹配除换行符之外的任何字符。 You need to escape it.
你需要逃脱它。 Use:
采用:
String k[] = sen.split("\\.");
Change: 更改:
sen.split(".");
To: 至:
sen.split("\\.");
You need to escape the dot, if you want to split on a dot: 如果要分割点,则需要转义点:
String k[] = sen.split("\\.");
A .
一
.
splits on the regex .
分裂正则表达式
.
, which means any character. ,这意味着任何角色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.