在Java中使用Split并对结果进行子串[重复]

时间:2021-11-22 21:44:11

Possible Duplicate:
string split in java

可能重复:java中的字符串拆分

I have this Key - Value , and I want to separate them from each other and get return like below:

我有这个Key - Value,我想将它们彼此分开并得到如下返回:

String a = "Key"
String b = "Value"

so whats the easiest way to do it ?

那么最简单的方法是什么?

7 个解决方案

#1


9  

String[] tok = "Key - Value".split(" - ", 2);
// TODO: check that tok.length==2 (if it isn't, the input string was malformed)
String a = tok[0];
String b = tok[1];

The " - " is a regular expression; it can be tweaked if you need to be more flexible about what constitutes a valid separator (e.g. to make the spaces optional, or to allow multiple consecutive spaces).

“ - ”是正则表达式;如果您需要更灵活地构建有效分隔符(例如,使空格可选,或允许多个连续空格),则可以进行调整。

#2


5  

String[] parts = str.split("\\s*-\\s*");
String a = parts[0];
String b = parts[1];

#3


3  

int idx = str.indexOf(" - ");
String a = str.substring(0, idx);
String b = str.substring(idx+3, str.length());

split() is a bit more computation intensive than indexOf(), but if you don't need to split billions of times per seconds, you don't care.

split()比indexOf()更加计算密集,但如果你不需要每秒分割数十亿次,你就不在乎了。

#4


2  

String s = "Key - Value";
String[] arr = s.split("-");
String a = arr[0].trim();
String b = arr[1].trim();

#5


2  

I like using StringUtils.substringBefore and StringUtils.substringAfter from the belowed Jakarta Commons Lang library.

我喜欢从下面的Jakarta Commons Lang库中使用StringUtils.substringBefore和StringUtils.substringAfter。

#6


1  

As a little bit longer alternative:

作为一个更长的替代方案:

    String text = "Key - Value";
    Pattern pairRegex = Pattern.compile("(.*) - (.*)");
    Matcher matcher = pairRegex.matcher(text);
    if (matcher.matches()) {
        String a = matcher.group(1);
        String b = matcher.group(2);
    }

#7


1  

something like

就像是

String[] parts = "Key - Value".split(" - ");
String a = parts[0];
String b = parts[1];

#1


9  

String[] tok = "Key - Value".split(" - ", 2);
// TODO: check that tok.length==2 (if it isn't, the input string was malformed)
String a = tok[0];
String b = tok[1];

The " - " is a regular expression; it can be tweaked if you need to be more flexible about what constitutes a valid separator (e.g. to make the spaces optional, or to allow multiple consecutive spaces).

“ - ”是正则表达式;如果您需要更灵活地构建有效分隔符(例如,使空格可选,或允许多个连续空格),则可以进行调整。

#2


5  

String[] parts = str.split("\\s*-\\s*");
String a = parts[0];
String b = parts[1];

#3


3  

int idx = str.indexOf(" - ");
String a = str.substring(0, idx);
String b = str.substring(idx+3, str.length());

split() is a bit more computation intensive than indexOf(), but if you don't need to split billions of times per seconds, you don't care.

split()比indexOf()更加计算密集,但如果你不需要每秒分割数十亿次,你就不在乎了。

#4


2  

String s = "Key - Value";
String[] arr = s.split("-");
String a = arr[0].trim();
String b = arr[1].trim();

#5


2  

I like using StringUtils.substringBefore and StringUtils.substringAfter from the belowed Jakarta Commons Lang library.

我喜欢从下面的Jakarta Commons Lang库中使用StringUtils.substringBefore和StringUtils.substringAfter。

#6


1  

As a little bit longer alternative:

作为一个更长的替代方案:

    String text = "Key - Value";
    Pattern pairRegex = Pattern.compile("(.*) - (.*)");
    Matcher matcher = pairRegex.matcher(text);
    if (matcher.matches()) {
        String a = matcher.group(1);
        String b = matcher.group(2);
    }

#7


1  

something like

就像是

String[] parts = "Key - Value".split(" - ");
String a = parts[0];
String b = parts[1];