2015-06-17 166 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