2016-05-16 75 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); 

希望它能幫助。

相關問題