如何在android中拆分字符串?

时间:2016-12-23 05:46:54

标签: android

我正在开发一个程序,我从服务器获得响应,如“1.user 2.name 3.mobile”。我必须按顺序排列这些值。我如何按顺序溢出这些值。

String i = "1.user 2.name 3.mobile";

我想要它的模式

1.USER

2.name

3.mobile

4 个答案:

答案 0 :(得分:0)

试试此代码

String i = "1.user 2.name 3.mobile";
String split[]=i.split(" ");
String firstValue=split[0];//1.user
String secondValue=split[1];//2.name
String thirdValue=split[2];//3.mobile

答案 1 :(得分:0)

        String i = "1.user 2.name 3.mobile";
        String[] array = i.split("\\s");
        for (int j = 0; j < array.length; j++) {
            System.out.println(array[j]); \\ will get like 1.user 
            String[] innerArray = array[j].split("\\.");\\if you want name and sequence split through "."
            for (int k = 0; k < innerArray.length; k++) {
                System.out.println(innerArray[k]); will get like \\ 1  , user
            }
        }

答案 2 :(得分:0)

您可以使用String.split功能

Undefined

答案 3 :(得分:0)

Tested and working!
        ArrayList<String> alData=new ArrayList<>();
        String i="1.user 2.name 3.email";  
        String[] array = i.split("\\b[0-9].");
        StringBuffer sb = new StringBuffer("");
         for(int j=1;j<array.length;j++)
           {
         //sb.append(j+1+""+array[j]+"\n ");
         alData.add(j+" "+array[j]);
         System.out.println(j+" "+array[j]);     
     }
       for(int k=0;k<alData.size();k++)
      {
      TextView tv=new TextView(mContext);
      tv.setText(alData.get(k));
      linearLayout.addView(tv);//you have to create a static linearLayout in your xml 
      }