Android(java)split包含特殊字符的字符串?

时间:2013-08-22 09:25:41

标签: java android string soap split

我正在创建一个从SOAP获取数据的应用程序。如果有数据,则必须将其放在列表视图中。 结果字符串是:

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 '| '.

但是现在我必须再次拆分字符串以获得三个字符串。但我无法弄清楚如何拆分它。我需要在':'处拆分该字符串,然后将其放在listview中。

如果有人知道如何分割字符串请告诉我!

3 个答案:

答案 0 :(得分: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();
        }
 }

答案 1 :(得分:0)

String selectedFromList = text.getText()。toString();                         String abc [] = selectedFromList.split(&#34;,&#34;); //%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();
                        }
                    }

答案 2 :(得分: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();
                        }
                    }