在java中的分隔符将字符串分割 [英] Split string in java for delimiter
问题描述
我的问题是,我想在Java中使用分隔符 ^
分割字符串。
和语法,我用的是:
My question is that I want to split string in java with delimiter ^
.
And syntax which I am using is:
readBuf.split("^");
但是,这并不拆分string.Infact这适用于所有其他分隔符而不是 ^
。
推荐答案
拆分
使用常规的前pressions(不幸的是,IMO)。 ^
在常规的前pressions特殊的意义,所以你要逃避它:
split
uses regular expressions (unfortunately, IMO). ^
has special meaning in regular expressions, so you need to escape it:
String[] bits = readBuf.split("\\^");
(需要的第一个反斜杠用于Java转义,实际字符串是只是单个反斜线和插入符号。)
(The first backslash is needed for Java escaping. The actual string is just a single backslash and the caret.)
另外,使用番石榴及其<一个href=\"http://guava-libraries.google$c$c.com/svn/trunk/javadoc/com/google/common/base/Splitter.html\"><$c$c>Splitter$c$c>类。
Alternatively, use Guava and its Splitter
class.
这篇关于在java中的分隔符将字符串分割的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!