2015-01-01 194 views
1

我想從masterArray中的字母中拆分數字,並將它們分別存儲在ArrayListsplitInteger,splitString)中。目前,我的號碼爲splitIntegerArrayList,但splitStringArrayList中的字母未正確分隔。使用正則表達式從字符串中分割數字

splitStringArrayList有額外的空格,逗號和ArrayList中的元素順序不正確。

輸出應該如下:

[55CC, 1C, 255D, 0F] 
[55, 1, 255, 0] 
[CC, C, D, F] 

目前,輸出如下:

[55CC, 1C, 255D, 0F] 
[55, 1, 255, 0] 
[, CC, , C, , D, , F] 

我試着與我的removeNumbers可變正則表達式搞亂,但是不知道很多關於正則表達式。

import java.util.ArrayList; 

public class SplitArrayExample { 

public static void main(String[] args) 
{ 
    int convertValue = 0; 

    // Using Regular Expressions to remove Characters 
    String removeLetters = "([A-z]+)"; 
    String removeNumbers = "([0-9]+)"; 

    // This is the master list. 
    ArrayList<String> masterArray = new ArrayList<String>(); 
     masterArray.add("55CC"); 
     masterArray.add("1C"); 
     masterArray.add("255D"); 
     masterArray.add("0F"); 

    // These are used to split the integer and string characters 
    ArrayList<Integer> splitInteger = new ArrayList<Integer>(); 
    ArrayList<String> splitString = new ArrayList<String>(); 

    for (String element : masterArray) 
    { 
     // Used to split the strings 
     String[] removingLetterCharacters = element.split(removeLetters); 
     String[] removingNumberCharacters = element.split(removeNumbers); 

     // This for-each loop removes letter values and converts numbers to integer values 
     for(String addObject : removingLetterCharacters) 
     { 
      convertValue = Integer.parseInt(addObject); 
      splitInteger.add(convertValue); 
     } 

     // This for-each loop removes the integer values 
     for(String addObject : removingNumberCharacters) 
     { 
      splitString.add(addObject); 
     } 
    } 

    System.out.println(masterArray); 
    System.out.println(splitInteger); 


    System.out.println(splitString); 
    } 
} 

回答

1

split用於獲取的字母列表,該列表將包含在原始列表中的每個元素兩個元素:一個空字符串,和實際的字符串。

例如,對於元素55CC,字母列表將包含[, CC]

爲了解決這個問題,一個辦法是在輸出列表中添加它之前檢查是否字符串爲空:

for (String addObject : removingLetterCharacters) { 
    if(!addObject.isEmpty()) { 
     convertValue = Integer.parseInt(addObject); 
     splitInteger.add(convertValue); 
    } 
} 

for (String addObject : removingNumberCharacters) { 
    if(!addObject.isEmpty()) { 
     splitString.add(addObject); 
    } 
} 

但是,使用如果原始名單有更復雜的組合split將無法​​正常工作字母和數字。例如,如果列表是[55CC7, 1C, 255D, 0F],那麼數字列表將包含數字而不是4,因爲split方法會將元素55CC7分成兩個數字557。更好的選擇是使用String#replaceAll以空字符串替換該模式。在這種情況下,正則表達式可以更簡單:

// Using Regular Expressions to remove Characters 
String removeLetters = "[A-z]"; 
String removeNumbers = "\\d"; 

// This is the master list. 
ArrayList<String> masterArray = new ArrayList<String>(); 
masterArray.add("7CC55"); 
masterArray.add("1C"); 
masterArray.add("255D"); 
masterArray.add("0F"); 


// These are used to split the integer and string characters 
ArrayList<Integer> splitInteger = new ArrayList<Integer>(); 
ArrayList<String> splitString = new ArrayList<String>(); 

for (String element : masterArray) { 
    String removingLetterCharacters = element.replaceAll(removeLetters, ""); 
    String removingNumberCharacters = element.replaceAll(removeNumbers, ""); 
    splitInteger.add(Integer.parseInt(removingLetterCharacters)); 
    splitString.add(removingNumberCharacters); 
} 
+0

謝謝@manouti,這個偉大工程! – ChaseHardin

+0

@YoungandFree檢查我的更新答案。 split可能不適用於更復雜的數字和字母組合。 – manouti

2

嘗試使用字符串的replaceAll方法,所以它刪除所有字符或數字從您的字符串,如:

String removeLetters = "([A-Z]+)";//you have typo here note capital Z 
for (String element : masterArray) { 
    String removingLetterCharacters = element.replaceAll(removeLetters, ""); 
    String removingNumberCharacters = element.replaceAll(removeNumbers, ""); 
    convertValue = Integer.parseInt(removingLetterCharacters); 
    splitInteger.add(convertValue); 
    splitString.add(removingNumberCharacters); 
} 

Output: 
[55CC, 1C, 255D, 0F] 
[55, 1, 255, 0] 
[CC, C, D, F] 
相關問題