2016-05-16 58 views
1

假设我有一个名为c的字符串,其中存储了“BPI-1111-2203-4493”。如何在java中的第n个“ - ”(短划线)之后获取字符串?

String c = "BPI-1111-2203-4493"; 

我现在想这个字符串分割成两个字符串是这样的:

String one = "BPI"; 
String two = "1111-2203-4493"; 

我曾尝试使用此:

String[] b = bank.split("-"); 
String one = b[0]; 
String two = b[1]; 

但字符串结束这样的:

String one = "BPI"; 
String two = "1111"; 

what String函数应该使用,以便字符串二将具有“1111-2203-4493”而不是“1111”。

感谢您的帮助!

+4

'indexOf'和'substring'? –

+0

@JonSkeet如果我知道哪个'-',那很容易。但是“_nth'-'_” – Hackerdarshi

+2

你可以多次调用'indexOf',每次传入前一个调用的结果来移动。 (查看'indexOf'的超载,它需要一个初始索引。) –

回答

5

您可以限制分裂

String c = "BPI-1111-2203-4493"; 
String[] s = c.split("-", 2); 
+0

问题是如何在第n个破折号上分割,例如n = 2意味着“BPI-1111”和“2203-4493”。 – Andreas

+0

@Andreas然后你可以做'c.split(“ - ”,3)[2]'这会给你'2203-4493' –

+0

但不是'BPI-1111'。 – Andreas

2

简单的解决办法是使用subStringindexOf方法如下:

String c = "BPI-1111-2203-4493"; 
String one = c.subString(0, c.indexOf('-')); 
String two = c.subString(c.indexOf('-') + 1, c.length()); 
+0

问题是如何在第n个划线上划分,例如n = 2意味着“BPI-1111”和“2203-4493”。 – Andreas

0

到第n破折号后拆分,使用常规表达式找到要分裂的点并呼叫substring()。只需将{}之间的数字更改为合适的n值即可。

(?:[^-]*-){2} 

这里是演示所有3个n:

String input = "BPI-1111-2203-4493"; 
Matcher m; 

// Split on 1st dash 
m = Pattern.compile("(?:[^-]*-){1}").matcher(input); 
if (m.find()) 
    System.out.printf("%s, %s%n", input.substring(0, m.end() - 1), 
            input.substring(m.end())); 

// Split on 2nd dash 
m = Pattern.compile("(?:[^-]*-){2}").matcher(input); 
if (m.find()) 
    System.out.printf("%s, %s%n", input.substring(0, m.end() - 1), 
            input.substring(m.end())); 

// Split on 3rd dash 
m = Pattern.compile("(?:[^-]*-){3}").matcher(input); 
if (m.find()) 
    System.out.printf("%s, %s%n", input.substring(0, m.end() - 1), 
            input.substring(m.end())); 

输出:

BPI, 1111-2203-4493 
BPI-1111, 2203-4493 
BPI-1111-2203, 4493 

可以很容易地变成一个可重用的helper方法:

private static String[] split(String input, char ch, int n) { 
    if (n <= 0) 
     throw new IllegalArgumentException("n must be >0"); 
    Matcher m = Pattern.compile("(?:[^" + ch + "]*" + ch + "){" + n + "}").matcher(input); 
    if (! m.find()) 
     return new String[] { input }; 
    return new String[] { input.substring(0, m.end() - 1), 
          input.substring(m.end()) }; 
} 

测试:

String input = "BPI-1111-2203-4493"; 
System.out.println(Arrays.toString(split(input, '-', 1))); 
System.out.println(Arrays.toString(split(input, '-', 2))); 
System.out.println(Arrays.toString(split(input, '-', 3))); 
System.out.println(Arrays.toString(split(input, '-', 4))); 

输出:

[BPI, 1111-2203-4493] 
[BPI-1111, 2203-4493] 
[BPI-1111-2203, 4493] 
[BPI-1111-2203-4493] 

也可以按照suggestion by Jon Skeet和使用indexOf()

private static String[] split(String input, char ch, int n) { 
    if (n <= 0) 
     throw new IllegalArgumentException("n must be >0"); 
    int idx = 0; 
    for (int i = 0; i < n; i++) 
     if ((idx = input.indexOf(ch, idx) + 1) == 0) 
      return new String[] { input }; 
    return new String[] { input.substring(0, idx - 1), 
          input.substring(idx) }; 
} 
-1

你的问题是,在你的情况下,分割方法返回一个包含四个项目的数组:

b[0] = "BPI" 
b[1] = "1111" 
b[2] = "2203" 
b[3] = "4493" 

我会建议一个类似以前的一些答案的方法,但与第一一些错误检查:

String c = "BPI-1111-2203-4493"; 
String one = null; 
String two = null; 
if((null != c)) { 
    int firstDashIndex = c.indexOf('-'); 
    if(firstDashIndex >= 0) { 
     one = c.substring(0, firstDashIndex); 
     if((firstDashIndex + 1) < c.length()) { 
      two = c.substring(firstDashIndex + 1); 
     } 
    } 
} 
System.out.println(one); 
System.out.println(two); 

希望它能帮助。

相关问题