如何在|上分割字符串(管道)在Java中 [英] How to split a string on | (pipe) in Java
问题描述
我有一个像 | serialNo | checkDelta?| checkFuture?| checkThis?|
之类的字符串.
I have a string like |serialNo|checkDelta?|checkFuture?|checkThis?|
.
现在我正在使用以下代码分割字符串.
Now I am using the following code to split the string.
String [] splitString = str.split("|");
但是当我使用它时,我得到包含每个字符的字符串数组,而我需要包含诸如 serialNo
, checkDelta?
,的字母的字符串checkFuture?
, checkthis?
.
but when I use this I get array of string that contains each and every character, whereas I need string which contains letter like serialNo
, checkDelta?
, checkFuture?
, checkthis?
.
如何获得这些?我想念什么吗?
How to get these? Am I missing something?
推荐答案
You'll have to escape your pipe character (split takes a regular expression as argument and therefore "|" is a control character):
str.split("\\|");
请注意:由于您使用"|",因此结果数组的开头包含一个空字符串.在字符串的开头.
Please note: the resulting array contains an empty string at the beginning since you have "|" at start of your string.
这篇关于如何在|上分割字符串(管道)在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!