2017-01-09 52 views
0

我想從每行提取一個指定索引的子字符串。這裏是字符串:每次遍歷一個字符串&提取子字符串的指定索引

String allPins = "16101702000000 376627817243 
       16101702000001 326520924472 
       16101702000000 376627817243 
       16101702000001 326520924472 
       16101702000000 376627817243 
       16101702000001 326520924472 
       16101702000000 376627817243 
       16101702000001 326520924472 
       16101702000000 376627817243 
       16101702000001 326520924472 
       16101702000000 376627817243 
       16101702000001 326520924472 
       16101702000000 376627817243 
       16101702000001 326520924472 
       16101702000000 376627817243 
       16101702000001 326520924472 
       16101702000000 376627817243 
       16101702000001 326520924472 
       16101702000000 376627817243 
       16101702000001 326520924472"; 

它不是一個字符串類型的數組。前14位是批號。我想提取第二個12位數字,即每次我循環字符串時,將引腳號作爲子字符串。

這裏是到目前爲止我的代碼:

for (int i = 0; i < allPins.length();i++){ 

     String str = allPins.substring((28*i)+15,(28*i)+28); 
     String test = str;  

     System.out.println(test); 
} 

//我正在一個outOfBoundError。

//請幫助我瞭解如何最好地提取子字符串。

+1

什麼約簡單地通過空格和斷行分裂? – SomeJavaGuy

+0

請在下次嘗試正確格式化您的代碼。 – TheLostMind

+0

使用帶有字邊界的正則表達式。檢查一個*精確的* 12位數字前面加一個空格並使用'Pattern'和'Matcher'類 – TheLostMind

回答

0

正如其他人已經評論過的,如果你只是用空格分隔字符串,你可能會有更容易的時間。在這種情況下,我會分裂爲\s+,意思是一個或多個空白字符。

String[] parts = allPins[i].split("\\s+"); 
for (int i=1; i < parts.length; i=i+2) {  // iterate and print odds only 
    System.out.println(parts[i]); 
} 

這種方法應該是罰款,如果你知道你的數據是良好的,即每個12位數的PIN碼始終是14位的批號之前(且僅由之前)。

如果你有不規則的數據,你的問題並不意味着,那麼你可能不得不與匹配器。

0
String str = allPins.substring((28*i)+15,(28*i)+28); 

上面一行是問題,當我值增加它會導致出界錯誤的,因爲你會大於所限定字符串的索引。

更好的方法是進行循環,並通過分割空間和檢查...

String str = allPins.substring(allPins.split(" ")[1].toString()); 
+0

感謝AurA,我嘗試着改正你的錯誤,但我得到一個不兼容的類型錯誤:字符串不能轉換爲int。 // [1] – Latz

+0

你可以對它進行字符串加我修正。 – AurA

0

I want extract the second 12-digit number, i.e. pin number as a substring everytime I loop through the string.

你甚至都不需要使用子方法。看到下面的例子:

for (String num: allPins.split(" ")) { 
    if(num.length() == 12) 
     System.out.println(num); 
    } 
+0

嗨VHS,謝謝你的幫助。我很困惑,你的代碼中的提取的引腳是子字符串。我確定num.length()== 12是一個布爾值。 – Latz

+0

@Latz,不客氣。關於你的其他問題,'split'給你所有的空格分隔的數字。但是,您有兩種類型的數字 - 一種是16位數字,另一種是12。後者是您的PIN碼。 – VHS

0

是的,你可以嘗試像@Kevin埃舍建議,請嘗試拆分字符串基於空格。這應該對你有所幫助。

嘗試用下面的代碼片斷:

對(INT I = 0;我< allPins.length();我++){

String[] str = allPins.split("\\s"); 
    String test = str[1];  

    System.out.println(test); 

}

+0

感謝Neelmani爲此分享了一些信息,我按照你的說法做了,但是我仍然無法打印所有的子字符串。我想我在迭代到下一行時遇到了困難。 – Latz

0

分割字符串將是這是一個更好的方法。

嘗試

String[] strArr = allPins.split(" "); 

More of white space splitting

現在你有一個字符串數組與您的批號和PIN碼相鄰。你可以通過遍歷一個for循環訪問它們。

還要記住要做的

string.equals("checking string") 

代替

string== string2 

檢查平等。

0

String [] s1 = allPins.split(「」); System.out.println(s1.length);

對(INT I = 0;我< s1.length/2 -1;我++){

String str = allPins.substring((28*i)+15,(28*i)+28); 
String test = str;  

System.out.println(test); 

}

+0

謝謝ashwani,非常感謝。但結果只是打印所需子字符串總數的一半。 – Latz