如何将字符串解析为字符串类型的不同arraylist
How to parse the string in to different arraylist of string type
我有一个字符串,其中包含 4 个东西,我想将每个东西都存储在一个列表中。
该字符串包含从 android 到服务器的食品订单,在服务器中我必须解析它才能显示。
字符串看起来像:
[:ordername:ordertime:orderprice: orderquantity:]
对于 1 个订单案例,我想在名称列表中输入订单名称,在时间列表中输入订单时间,在价目表中输入订单价格等
如果超过 1 个订单,它将用逗号分隔,如
[:ordername:ordertime:orderprice:orderquantity:],[:ordername2:ordertime2:orderprice2:orderquantity2:]
我想在名单中输入 ordername
,ordername2
并在时间列表中输入 ordertime
, ordertime2
以及 orderprice
, orderprice2
在价目表等。
这是我试过的
String orderlist=request.getParameter("orderlist"); // this is a string which is coming from android to server containing orders
char[] orderarray=orderlist.toCharArray(); //converting string to char array
int comma_counter = 0;
int comma_counter1 = 0;
for (int i=0; i < orderlist.length(); i++){
if (orderarray[i]==','){
comma_counter++;
}
}
System.out.println("counter is"+comma_counter);
System.out.println("order list length"+orderlist.length());
ArrayList <String> order_array_list = new ArrayList <String>();
int no=0;
String temp="";
for (int j=no; j<orderarray.length; j++){
System.out.println(" j is "+ j);
if(orderarray[j]!=','){
temp = temp+orderarray[j];
// System.out.println("temp is "+temp);
}
else if(orderarray[j]==','){
order_array_list.add(temp);
temp="";
no=j;
}
}
String []parts= null;
for(int i=0; i<order_array_list.size(); i++){
String array= order_array_list.get(i);
parts= array.split(":");
for(int j=0; j<parts.length; j++)
{
System.out.println(parts[j]);
}
}
像这样的东西会起作用:
Map <Integer, List <String>> map = new HashMap <>();
// Initialize the map
map.put(1, new ArrayList <String> ());
map.put(2, new ArrayList <String> ());
map.put(3, new ArrayList <String> ());
map.put(4, new ArrayList <String> ());
String str = "[:ordername:ordertime:orderprice:orderquantity:]," +
"[:ordername2:ordertime2:orderprice2:orderquantity2:]";
// loop through each order set
for (String s: str.split(","))
{
// remove any leading and trailing spaces
s = s.trim();
// remove the brackets
s = s.replaceAll("[\[\]]", "");
int i = 1;
// loop through each order component
for (String c: s.split(":"))
{
// remove any leading and trailing spaces
c = c.trim();
if (c.length() > 0)
{
map.get(i).add(c);
i++;
}
}
}
System.out.println(map);
输出:
{1=[ordername, ordername2], 2=[ordertime, ordertime2],
3=[orderprice, orderprice2], 4=[orderquantity, orderquantity2]}
注意:为简单起见,我使用 HashMap
来包含所有列表,但您可以在 [=11= 之外创建 4 个列表] 如果你希望。在这种情况下,您需要在 if (c.length() > 0)
语句中包含 if/else if
条件。
希望此代码对您有所帮助
import java.util.ArrayList;
import java.util.List;
public class MainClass {
public static void main(String[] args) {
String input = "[:ordername:ordertime:orderprice:orderquantity:], [:ordername2:ordertime2:orderprice2:orderquantity2:]";
input = input.replace("[:", "");
input = input.replace(":]", "");
String[] inputArray = input.split(",");
List<String> nameList = new ArrayList<String>();
List<String> timeList = new ArrayList<String>();
List<String> priceList = new ArrayList<String>();
List<String> quantityList = new ArrayList<String>();
for (String order : inputArray) {
String[] orderDetails = order.split(":");
nameList.add(orderDetails[0]);
timeList.add(orderDetails[1]);
priceList.add(orderDetails[2]);
quantityList.add(orderDetails[3]);
}
}
}
如果您更关心性能,您可以使用 apache commons 来替换字符串。
我有一个字符串,其中包含 4 个东西,我想将每个东西都存储在一个列表中。 该字符串包含从 android 到服务器的食品订单,在服务器中我必须解析它才能显示。 字符串看起来像:
[:ordername:ordertime:orderprice: orderquantity:]
对于 1 个订单案例,我想在名称列表中输入订单名称,在时间列表中输入订单时间,在价目表中输入订单价格等 如果超过 1 个订单,它将用逗号分隔,如
[:ordername:ordertime:orderprice:orderquantity:],[:ordername2:ordertime2:orderprice2:orderquantity2:]
我想在名单中输入 ordername
,ordername2
并在时间列表中输入 ordertime
, ordertime2
以及 orderprice
, orderprice2
在价目表等。
这是我试过的
String orderlist=request.getParameter("orderlist"); // this is a string which is coming from android to server containing orders
char[] orderarray=orderlist.toCharArray(); //converting string to char array
int comma_counter = 0;
int comma_counter1 = 0;
for (int i=0; i < orderlist.length(); i++){
if (orderarray[i]==','){
comma_counter++;
}
}
System.out.println("counter is"+comma_counter);
System.out.println("order list length"+orderlist.length());
ArrayList <String> order_array_list = new ArrayList <String>();
int no=0;
String temp="";
for (int j=no; j<orderarray.length; j++){
System.out.println(" j is "+ j);
if(orderarray[j]!=','){
temp = temp+orderarray[j];
// System.out.println("temp is "+temp);
}
else if(orderarray[j]==','){
order_array_list.add(temp);
temp="";
no=j;
}
}
String []parts= null;
for(int i=0; i<order_array_list.size(); i++){
String array= order_array_list.get(i);
parts= array.split(":");
for(int j=0; j<parts.length; j++)
{
System.out.println(parts[j]);
}
}
像这样的东西会起作用:
Map <Integer, List <String>> map = new HashMap <>();
// Initialize the map
map.put(1, new ArrayList <String> ());
map.put(2, new ArrayList <String> ());
map.put(3, new ArrayList <String> ());
map.put(4, new ArrayList <String> ());
String str = "[:ordername:ordertime:orderprice:orderquantity:]," +
"[:ordername2:ordertime2:orderprice2:orderquantity2:]";
// loop through each order set
for (String s: str.split(","))
{
// remove any leading and trailing spaces
s = s.trim();
// remove the brackets
s = s.replaceAll("[\[\]]", "");
int i = 1;
// loop through each order component
for (String c: s.split(":"))
{
// remove any leading and trailing spaces
c = c.trim();
if (c.length() > 0)
{
map.get(i).add(c);
i++;
}
}
}
System.out.println(map);
输出:
{1=[ordername, ordername2], 2=[ordertime, ordertime2], 3=[orderprice, orderprice2], 4=[orderquantity, orderquantity2]}
注意:为简单起见,我使用 HashMap
来包含所有列表,但您可以在 [=11= 之外创建 4 个列表] 如果你希望。在这种情况下,您需要在 if (c.length() > 0)
语句中包含 if/else if
条件。
希望此代码对您有所帮助
import java.util.ArrayList;
import java.util.List;
public class MainClass {
public static void main(String[] args) {
String input = "[:ordername:ordertime:orderprice:orderquantity:], [:ordername2:ordertime2:orderprice2:orderquantity2:]";
input = input.replace("[:", "");
input = input.replace(":]", "");
String[] inputArray = input.split(",");
List<String> nameList = new ArrayList<String>();
List<String> timeList = new ArrayList<String>();
List<String> priceList = new ArrayList<String>();
List<String> quantityList = new ArrayList<String>();
for (String order : inputArray) {
String[] orderDetails = order.split(":");
nameList.add(orderDetails[0]);
timeList.add(orderDetails[1]);
priceList.add(orderDetails[2]);
quantityList.add(orderDetails[3]);
}
}
}
如果您更关心性能,您可以使用 apache commons 来替换字符串。