本文介绍了用|分割字符串java中的分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,如下所示: 1 |value|;

I have a string that's like this: 1|"value"|;

我想拆分该字符串并选择 | 作为分隔符。

I want to split that string and have chosen | as the separator.

我的代码如下所示:

String[] separated = line.split("|");

我得到的是一个包含所有字符作为一个条目的数组:

What I get is an array that contains all characters as one entry:

separated[0] = ""
separated[1] = "1"
separated[2] = "|"
separated[3] = """
separated[4] = "v"
separated[5] = "a"
...

有谁知道为什么?

我不能用 | ?

Does anyone know why?
Can't I split an string with |?

推荐答案

| 被视为在RegEx中。所以你需要逃避它:

| is treated as an OR in RegEx. So you need to escape it:

String[] separated = line.split("\\|");

这篇关于用|分割字符串java中的分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:03
查看更多