2015-06-17 165 views
0

在我的應用程序中,我將聯繫人保存在SharedPreferences中,但我傳遞的數組還包括字符串中的開放大括號和雙引號。從數組中移除大括號和雙引號

這樣[ 「contactnumber1」, 「contactnumber2」, 「contactnumber3」 .......]

但我需要字符串是這樣 contactnumber1,contactnumber2,contactnumber3 ....

這樣我就可以使用這個contactnumbers字符串直接將其放入短信應用程序並通過單擊按鈕發送警報消息。

請幫我這個

代碼:

String[] arrayOfString=localMultiAutoCompleteTextview.getText().toString().split(","); 

// Create a JSONArray to store all numbers 
      JSONArray numberArray=new JSONArray(); 

// Loop through the multiautocomplete textview value array 
      for(int i=0; i < arrayOfString.length; i++) 
      { 
       // Check whether the string contains '%' 
       if(arrayOfString[i].contains("%")) 
       { 
        // Add numbers to the array 
        numberArray.put(arrayOfString[i].split("%")[1]); 
       } 
      } 

// Store the complete number array in preference as String 
      sp=getActivity().getSharedPreferences("sdat", 2); 
      ed=sp.edit(); 
      ed.putString("snum", numberArray.toString()); 
      ed.commit(); 


// To read the numbers after saving 
      String display = sp.getString("snum", new JSONArray().toString()); 
      System.out.println(display); 
      Toast.makeText(getActivity(),"Contacts Saved:"+display,Toast.LENGTH_SHORT).show(); 
     } 
    }); 
+0

你不能在一個數組存儲字符串無引用 –

回答

0

下面一行是一個正則表達式相匹配的報價和括號,並從字符串中刪除。只要打電話replaceAll,它會通過你的字符串,找到如下字符:「[」,「]」,「「」,並從字符串中刪除他們

strWithoutQuotesAndBraces = strWithQuotesAndBraces.replaceAll("(["[\\]])", ""); 
+0

如何在我的代碼中調用它PLZ告訴我...作爲我正在將字符串存儲在共享偏好中。何時何地應添加上面的replaceAll。 – sharath0033