2015-07-21 54 views
0

我使用下面的代碼進行拆分。我怎樣才能拆分「,」即使它有一個「,」在其中

List<String> separatedStringList = Arrays.asList(line.split(",")); 

然後我解析字符串存儲到

ArrayList<String> tags; 
ArrayList<String> values; 

但是,當我有如下一行:

abc: def,xyz: pqr, uvw: abchash<class qwerty,struct Hash<class qwerty>,struct std::equal_to<class qwerty> >::resize 

上的分裂「」引起uvw: abchash<class qwerty, struct Hash<class qwerty>, struct std::equal_to<class qwerty>>::resize問題部分。

如何解決這個問題?

在延續的問題here

+0

你的問題是有點混亂。你說你不想拆分uvw:abchash ,struct std :: equal_to > resize part? – JFPicard

+0

@JFPicard - 這是正確的。由於uvw的值是整行,直到調整大小,我需要它作爲值,而不是直到第一個「,」。 – Vidya

+0

我的回答對你的問題有幫助嗎?如果是,請標記爲已解決。 –

回答

0

評估您的字符串,其基於堆棧的算法爲通用類型約束如下:

代碼

final String text = "abc: def,xyz: pqr, uvw: abchash<class qwerty,struct Hash<class qwerty>,struct std::equal_to<class qwerty> >::resize"; 

int start = 0; 
Stack<Character> generics = new Stack<>(); 
for (int i = 0; i < text.length(); i++) { 
    char c = text.charAt(i); 
    switch (c) { 
     case '<': 
      generics.push(c); 
      break; 
     case '>': 
      if(generics.isEmpty() || generics.pop() != '<') { 
       throw new IllegalArgumentException("Invalid generic type constraints"); 
      } 
      break; 
     case ',': 
      if (generics.isEmpty()) { 
       System.out.println(text.substring(start, i)); 
       start = i + 1; //move window 
      } 
      break; 
    } 
} 
if (generics.isEmpty()) { 
    System.out.println(text.substring(start)); 
} 

輸出

abc: def 
xyz: pqr 
uvw: abchash<class qwerty,struct Hash<class qwerty>,struct std::equal_to<class qwerty> >::resize 

個Futher改進
上面的代碼可以寫成一個Iterable

public static Iterator<String> iterateValues(String text) { 
    return new Iterator<String>() { 
     int start = 0; 
     Stack<Character> generics = new Stack<>(); 

     @Override 
     public boolean hasNext() { 
      return start <= text.length(); 
     } 

     @Override 
     public String next() { 
      for (int i = start; i < text.length(); i++) { 
       char c = text.charAt(i); 
       switch (c) { 
        case '<': 
         generics.push(c); 
         break; 
        case '>': 
         if (generics.isEmpty() || generics.pop() != '<') { 
          throw new IllegalArgumentException("Invalid generic type constraints"); 
         } 
         break; 
        case ',': 
         if (generics.isEmpty()) { 
          String result = text.substring(start, i); 
          start = i + 1; //move window 
          return result; 
         } 
         break; 
       } 
      } 
      String result = text.substring(start); 
      start = text.length() + 1; //move window 
      return result; 
     } 
    }; 
} 
0

您可以分割上「」並檢查4字符是‘:’。在這種情況下,你把數組,如果沒有,你把陣列和其他...

像這樣:

String[] parts = string.split(","); 
int i = 0; 
while (i < parts.length) { 
    if (parts[i].charAt(4) == ':') { 
    //Do something with parts[i] 
    i++; 
    } 
    else { 
    String wantedPart = parts[i]; 
    i++; 
    while(parts[i].charAt(4) != ':' && i < parts.length) { 
     wantedPart = wantedPart + "," + parts[i]; 
     i++; 
    } 
    //Do something with wantedPart 
    } 
} 

這是一般的想法。當然,如果wantedPart很大,你可能會想要使用一個StringBuilder。

+0

我假設給定的字符串只是一個例子,並且在第四個位置並不總是有一個':'。我敢肯定,你會得到一個ArrayIndexOutOfBoundsException這個代碼 - while while循環在while循環中循環相同的事情不是一個好的設計。你需要插入錯誤地將''''移回到字符串中。 – Dukeling

+0

我試着讓它快速,但你是對的,我的第二個循環可能會拋出一個ArrayIndexOutOfBounds。我糾正了它。對於',',你的權利也。 – JFPicard

0

如果我正在閱讀這個權利,你得到的問題是一個格式不正確的輸入字符串,你在鍵值對的值部分有一個','。

所以你希望你的輸入

abc: def,xyz: pqr, uvw: abchash<class qwerty,struct Hash<class qwerty>,struct std::equal_to<class qwerty> >::resize 

解析到

abc: def 
xyz: pqr 
uvw: abchash<class qwerty,struct Hash<class qwerty>,struct std::equal_to<class qwerty> >::resize 

但是,相反它的解析來

abc: def 
xyz: pqr 
uvw: abchash<class qwerty 
struct Hash<class qwerty> 
struct std::equal_to<class qwerty> >::resize 

對此的解決方案是修復您的輸入值引用整個值字符串或使用其他分隔符給你一些規則。

據我可以告訴你基本上沒有關於你進入的規則。它可以是一個關鍵:值對或只是一個值,就像你在另一個問題中的例子。在這種情況下,不可能區分包含逗號的值與一個值的結尾和另一個值的開始之間的差異。你說這該字符串

abc:xyz uvw, def:ghi, mno:rst, ijk:efg, abc opq 

應解析成

abc:xyz uvw 
def:ghi 
mno:rst 
ijk:efg 
abc opq <-- no key, just a value 

這意味着該

abc: def,xyz: pqr, uvw: abchash<class qwerty,struct Hash<class qwerty>,struct std::equal_to<class qwerty> >::resize 

可以解析到

abc: def 
xyz: pqr 
uvw: abchash<class qwerty,struct Hash<class qwerty>,struct std::equal_to<class qwerty> >::resize 

或同樣

abc: def 
xyz: pqr 
uvw: abchash<class qwerty 
struct Hash<class qwerty>,struct std::equal_to<class qwerty> >::resize 

或同樣

abc: def 
xyz: pqr 
uvw: abchash<class qwerty 
struct Hash<class qwerty> 
struct std::equal_to<class qwerty> >::resize 

等等等等

當你有你需要輸入改變以某種方式讓你縮小正確的正則表達式用多個同樣有效的答案。