如何从Android中使用Java从服务数据中提取的字符串中删除双引号?

时间:2013-12-18 06:07:05

标签: java android web-services arraylist spinner

我是来自服务的gettig数据,并且从我以此格式删除数据后。

我已将数据保存在这样的数组中:

  

字符串数据数组(24150):1,“产品组”,“12345”,“服务备件”

     

字符串数据数组(24150):2,“产品组”,“12345”,“不再使用”

     

字符串数据数组(24150):3,“产品组”,“12345”,“门禁条目”

现在我想从这个数组中提取String **产品组,如果我已经初始化的字符串等于产品组,然后在最后一个数组列表中保存文本,(逗号)没有双引号。这是代码,但它返回我“”我如何删除双引号请指导我... **。

    for(int i=0;i<spinnerDataArraylocal.length;i++)
        {
          Log.i("String data array",""+spinnerDataArraylocal[i]);
           int index1 = spinnerDataArraylocal[i].indexOf(",");
         String labelString = spinnerDataArraylocal[i].substring(index1+1,spinnerDataArraylocal[i].indexOf(",",index1+1));
        Log.i("first comma position:",""+labelString);
        if(labelString.equals("\"Premises Type\""))
         {   int index = spinnerDataArraylocal[i].lastIndexOf(",");
             Log.i("string compare","labelString" + labelString);
             Log.i("extrACT STRING:" , spinnerDataArraylocal[i].substring(index+1));
             String sample = spinnerDataArraylocal[i].substring(index+1);
             Log.i("sample",""+sample);
             premisesTypeArrayList.add(sample.toString().trim());
             Log.i("premisesTypeArrayList",""+premisesTypeArrayList);
            ArrayAdapter<String> adapterPremisesType = new ArrayAdapter<String>(
                    getActivity().getApplicationContext(),R.layout.custom_spinner,
                    premisesTypeArrayList);
            adapterPremisesType.setDropDownViewResource(R.layout.custom_spinner_item_list);
 spinnerPremisesType.setAdapter(adapterPremisesType);
spinnerPremisesType.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
  public void onItemSelected(AdapterView<?> parent,
  View view, int position, long id) {

   }
  @Override
public void onNothingSelected(AdapterView<?> arg0) {
  }
 });
adapterPremisesType.notifyDataSetChanged();
 }

我需要将此代码重写为:if(labelString.equals("Premises Type"))而不是此

if(labelString.equals("\"Premises Type\""))

这是我的下拉数据填充的方式,应该没有双引号和空格

DROPDOWN IMAGE

2 个答案:

答案 0 :(得分:0)

这样做string =string.replaceAll("\"", "");

下面的拆分代码。

String string = "41,\"Premises Type\",\"A\",\"Retail\"";        
string =string.replaceAll("\"", "");
String[] array =string.split(",");
System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);
System.out.println(array[3]);

您的完整代码如下所示

for(int i=0;i<spinnerDataArraylocal.length;i++)
{
        String spinData = spinnerDataArraylocal[i];
        spinData = spinData.replaceAll("\"", "");
        String spinData[] = spinData.split(",");
        if(spinData[1].trim().equals("Premises Type"))
        {   
            String sample = spinData[1]+","+spinData[2]+","+spinData[3];
            premisesTypeArrayList.add(sample.toString().trim());            
            ArrayAdapter<String> adapterPremisesType = new ArrayAdapter<String>(
                    getActivity().getApplicationContext(),R.layout.custom_spinner,
                    premisesTypeArrayList);
            adapterPremisesType.setDropDownViewResource(R.layout.custom_spinner_item_list);
            spinnerPremisesType.setAdapter(adapterPremisesType);
            spinnerPremisesType.setOnItemSelectedListener(new OnItemSelectedListener() {
                @Override
                 public void onItemSelected(AdapterView<?> parent,  View view, int position, long id) {

                 }
                 @Override
                 public void onNothingSelected(AdapterView<?> arg0) {
                }
            });
            adapterPremisesType.notifyDataSetChanged();
      }

答案 1 :(得分:0)

为您完整的代码

public class Tets  {


   public static void main(String [] args) {
       String  data  = "3,\"product group\",\"12345\",\"Door Entry\"";        
       String [] items =data .split(",");
       System.out.println(items[0]);
       System.out.println(items[1].substring (1,items[1].length()-1));
       System.out.println(items[2].substring (1,items[2].length()-1));
       System.out.println(items[3].substring (1,items[3].length()-1));
   }


}
祝你好运