2013-08-22 134 views
1

我正在製作一個從SOAP獲取數據的應用程序。當它有數據時,它必須把它放在一個列表視圖中。 結果字符串是:Android(java)拆分包含特殊字符的字符串?

it_id=636207115 :#=1:price=1,18|it_id=636207115 :#=1:price=1,18|it_id=636205395 :#=1:price=0,92

我現在要分割字符串是這樣的:it_id=636207115 :#=1:price=1,18,因爲你可以看到string splits at the '| '.

但現在我不得不重新分割字符串拿到三串從那。但我不知道如何分割它。我需要將該字符串拆分爲':'並將其放入列表視圖中。

如果有人知道如何拆分字符串請讓我知道!

回答

1

首先Encode your String然後使用spilt()和比Decode String到原始

try { 
    str = URLEncoder.encode("t_id=636207115 :#=1:price=1,18|it_id=636207115 :#=1:price=1,18|it_id=636205395 :#=1:price=0,92", "UTF-8"); 
    } 
catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

String abc[] = str.split("%7C"); // %7C is Encoded | by which you want to spilt the String 

// Loop Through the Array and Decode the String ! 


for (int i = 0; i < abc.length; i++) { 
    try { 
     abc[i] = URLDecoder.decode(abc[i], "UTF-8");// Decoding String and Stroring it back to Array 
     System.out.println(abc[i]);// Testing String 
     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
} 
+0

謝謝!這會幫助我! –

+0

@MaikelR很高興它幫助你! –

0

字符串selectedFromList = text.getText()的toString(); String abc [] = selectedFromList.split(「,」); //%7C已編碼|通過要灑字符串

    for (int i = 0; i < abc.length; i++) { 
         try { 
          if(i==abc.length-1) 
          Log.i("deepika deepika ::", abc[i]); 
          tvCountryName.setText(abc[i]); 
          edit.putString(COUNTRY, abc[i]); 
          edit.commit(); 

         } catch (Exception e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        } 
0
    String selectedFromList = "hello, hi, how are you, guys"; 
        String abc[] = selectedFromList.split(","); 
        for (int i = 0; i < abc.length; i++) { 
         try { 

          Log.i("deepika deepika ::", abc[i]); 


         } catch (Exception e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        } 
相關問題