也许我很笨,但我不明白为什么 StringTokenizer 的行为在这里:
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml;
String object = (String) value;
String escaped = escapeHtml(object);
StringTokenizer tokenizer = new StringTokenizer(escaped, escapeHtml("<br/>"));
如果外汇。值为
Hej<br/>$user.get(0).name Har vundet<br/><table border='1'><tr><th>Name</th><th>Played</th><th>Brewed</th></tr>#foreach( $u in $user )<tr><td>$u.name</td> <td>$u.played</td> <td>$u.brewed</td></tr>#end</table><br/>
那么结果就是
Hej
$use
.
e
(0).name Ha
vunde
a
e
o
de
='1'
h
Name
h
h
P
ayed
h
h
B
ewed
h
#fo
each( $u in $use
)
d
$u.name
d
d
$u.p
ayed
d
d
$u.
ewed
d
#end
a
e
这对我来说毫无意义。
我怎样才能让它像我期望的那样运行。
最佳答案
来自documentation :
The characters in the delim argument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens.
换句话说,告诉 StringTokenizer
何时分隔字符串的字符是:
- <
- b
- r
- /
- >
当它匹配字符串中的任何这些字符(代码中的变量 escaped
)时,StringTokenizer
实例将拆分结果并丢弃标记。您可以通过注意输出中未出现字母 r
来确认这一点。
使用String.split
,相反,正如其他人所建议的那样。
关于java - StringTokenizer 在 "<br/>"拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4556414/