在java中拆分字符串

时间:2015-01-31 09:20:51

标签: android arrays string split

String items={shoporder=shop1,shopname=abc,place=mmm};

我想将项目拆分为(shop1)。并分配给另一个字符串

String1=shop1 
string2=abc
string3=mmm


 // Get User records from SQLite DB
    ArrayList<HashMap<String, String>> userList = controller.getAllUsers();
    // If users exists in SQLite DB
    if (userList.size() != 0) {
        // Set the User Array list in ListView
              final ListAdapter adapter = new SimpleAdapter(second.this, userList, R.layout.view_user_entry, new String[] {
                "shopId", "shopOrder","date","todayTarget","targetCompleted","shopHandled_person","shopName","salesmanName" }, new int[]
                { R.id.shopId,R.id.shopOrder,R.id.date,R.id.todayTarget,R.id.targetCompleted,R.id.shopHandled_person,R.id.shopName,R.id.salesmanName});
       final ListView myList = (ListView) findViewById(android.R.id.list);
        myList.setAdapter(adapter);
         myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override

             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                 String items=myList.getItemAtPosition(position).toString();
}

字符串项包含所有列表值。所以我想拆分值并传入第二个活动。

1 个答案:

答案 0 :(得分:0)

初始化String数组的正确方法是,

String[] items=new String[]{"shopOrder=shop1","shopname=abc","place=mmm"};

更简单的方法是使用split的{​​{1}}方法将字符串拆分为String

=方法会将您的split分为两部分,一部分在String之前,第二部分在=之后,即=&lt; - 部分之前{ {1}}签名,yourString.split("=")[0]&lt; - =签署后的部分。

yourString.split("=")[1]

您可以使用=查找字符串for (int i =0; i<items.length; i++){ System.out.println(items[i].split("=")[1]); } 后的所有内容,然后您可以使用yourString.indexOf("=")+1打印=个符号后的所有内容, substring

=
两种情况下

输出

substring(int beginIndex, int endIndex)