简体   繁体   English

按字符拆分字符串

[英]Split String By Character

I have a case in which I'm doing the following: 我有一个案例,我正在做以下事情:

final String[] columns = row.split(delimiter.toString());

Where delimiter is a Character. delimiter是字符的位置。

This works fine when I need to split based on tabs by providing \\t as the delimiter. 当我需要通过提供\\t作为分隔符来基于选项卡进行拆分时,这可以正常工作。 However, when I want to split on a pipe, I pass in a delimiter of | 但是,当我想拆分管道时,我传入了|的分隔符 and this does not work as expected. 这不能按预期工作。

I've read several posts about how | 我读了几篇关于如何|帖子 is a special character which means null or empty therefore it splits on every character it encounters, though, I don't want this behavior. 是一个特殊的字符,意味着null或空,因此它会分裂它遇到的每个字符,但是,我不想要这种行为。

I could do a simple check in my code for this pipe case and get around the issue: 我可以在我的代码中对此管道案例进行简单检查并解决问题:

if ("|".equals(delimiter.toString())) {
    columns = row.split("\\" + delimiter.toString());
}
else {
    columns = row.split(delimiter.toString());
} 

But I didn't know if there was an easier way to get around this. 但我不知道是否有更简单的方法来解决这个问题。 Also, are there any other special characters that act like the | 此外,是否有任何其他特殊字符的行为像| does that I need to take into account? 这是我需要考虑的吗?

Try: 尝试:

import java.util.regex.Pattern;

...

final String[] columns = row.split(Pattern.quote(delimiter.toString()));

With regards to the other metacharacters , as they're called, here's a quote from the String Literals tutorial: 关于其他元字符 ,当它们被调用时,这里是String Literals教程的引用:

This API also supports a number of special characters that affect the way a pattern is matched. 此API还支持许多影响模式匹配方式的特殊字符。

... ...

The metacharacters supported by this API are: <([{\\^-=$!|]})?*+.> 此API支持的元字符为:<([{\\ ^ - = $!|]})?* +。>

See: 看到:

  1. You can use StringUtils from Apache Commons Lang which is equipped with methods accepting plain text, not regular expressions: 您可以使用Apache Commons Lang中的StringUtils ,它配备了接受纯文本而非正则表达式的方法:

     public static String[] split(String str, char separatorChar) public static String[] split(String str, String separatorChars) 
  2. You can also use the StringTokenzier class, which does not expect a regular expression as the delimiter. 您还可以使用StringTokenzier类,它不期望正则表达式作为分隔符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM