简体   繁体   English

Java Android-如何使用内部的空数据拆分字符串数据

[英]Java android - How to split string data with empty data inside of it

I have a data string name stringSplit that I want to split into 5 parts. 我有一个数据字符串名称stringSplit,我想分成5部分。

I use this to split the String: 我用它来分割字符串:

String[] pisah = stringSplit.split(",", -1);
for (int i=0;i< pisah.length;i++) {
   String hasil1 = pisah[0];
   String hasil2 = pisah[1];
   String hasil3 = pisah[2];
   String hasil4 = pisah[3];
   String hasil5 = pisah[4];
}

When the value in stringSplit there are 5 data (fe: “data1,data2,data3,data4,data5”), the code is working properly. 当stringSplit中的值有5个数据(例如:“ data1,data2,data3,data4,data5”)时,代码工作正常。 But when in stringSplit there are only one, two or three data, an exception will caught: 但是,当stringSplit中只有一个,两个或三个数据时,将捕获一个异常:

(f.e: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4).

And my question is how to handle the empty string? 我的问题是如何处理空字符串? So even though the data only one, two, or three data the stringSplit still can be splitted. 因此,即使只有一个,两个或三个数据,stringSplit仍然可以拆分。

I've tried by adding limit to -1 which this solution I got from another post, but still not working for me. 我试图通过将限制添加到-1来解决,该解决方案是我从另一篇文章中获得的,但仍然对我不起作用。

Editted: 编辑:

I almost forgot, the result string (hasil1 - hasil5) will use for edittext. 我几乎忘记了,结果字符串(hasil1-hasil5)将用于edittext。

fe: editText1.setText(hasil1); fe:editText1.setText(hasil1);

and others. 和别的。

Here is the problem, 这是问题所在

for (int i=0;i< pisah.length;i++) {
   String hasil1 = pisah[0];
   String hasil2 = pisah[1];
   String hasil3 = pisah[2];
   String hasil4 = pisah[4];
   String hasil5 = pisah[5];
}

but that iterates over the list and gets the values in the list from 0-5 every time it loops, so what you should be doing is something like this 但这会遍历列表,并在每次循环时从0-5获取列表中的值,因此您应该执行的操作是这样的

for(int i = 0; i < 6; i++){
    if(i >= pisah.length()) break;  
    String hasilI = pisah[i];
}

This iterates over the list 6 times then if the list's size < 6 it will break out of the loop 这会遍历列表6次,然后如果列表的大小<6,则会跳出循环

One possible solution is to check if the index you are looking for is less than the length of the array. 一种可能的解决方案是检查您要查找的索引是否小于数组的长度。 If that index is less than the size of the array than it definitely exists. 如果该索引小于数组的大小,则肯定存在。

for (int i=0;i< pisah.length;i++) {
  if(0 < pisah.length)
     String hasil1 = pisah[0];
  if(1 < pisah.length)
     String hasil2 = pisah[1];
  //similarly for all other idexes that you want the value for.
}

use this code: 使用此代码:

 String res[] = new String[5];

String[] pisah = stringSplit.split(",");

for (int i=0;i< pisah.length && i < res.length;i++) {
  res[i] = pisah[i];
}

if(res[0] != null )
  //.......... res[0] valid 

if(res[1] != null )
  //.......... res[1] valid 

if(res[2] != null )
  //.......... res[2] valid 

if(res[3] != null )
  //.......... res[3] valid 

if(res[4] != null )
  //.......... res[4] valid 

You have hard coded the number of EditText fields you can have! 您已经硬编码了可以拥有的EditText字段的数量! What if, there is a change in requirement, where you have to add another field? 如果需求发生变化,您必须在其中添加另一个字段怎么办? That would need you to add "editText6" and "hasil6" at a lot of places. 那将需要您在很多地方添加“ editText6”和“ hasil6”。 The code is not nimble footed to changes in specs. 该代码不适用于规格更改。

This is what I would do. 这就是我要做的。

  1. Create a list of EditText, say, textList 创建一个EditText列表,例如textList
  2. Iterate over the pisah array, and textist simultaneously and add the String at index i in pisah to the EditText object at index i in textList. 遍历pisah数组,并同时进行textist操作,然后将pisah中索引i处的String添加到textList中索引i处的EditText对象。

This way, in case there are only 3 data values, you are automatically taking care of populating only 3 EditText objects. 这样,在只有3个数据值的情况下,您将自动处理仅填充3个EditText对象的情况。

This is the code : 这是代码:

List<EditText> textList;
String[] pisah = stringSplit.split(",", -1);
for (int i=0;i< pisah.length;i++) {
           textList.get(i).setTest(pisah[i]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM