如何拆分字符串 |(管道)在 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?
推荐答案
您必须转义管道字符 (split 将正则表达式作为参数,因此 "|" 是 控制字符):
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屋!