【问题标题】:Java splitting string using delimiter and store to different arrayJava使用分隔符拆分字符串并存储到不同的数组
【发布时间】:2021-08-03 07:03:21
【问题描述】:

我在尝试使用分隔符拆分字符串并存储到数组时遇到了一些问题。所以基本上我有一个主数组,输入如下:

1564095_SINGLE_true, 1564096_SINGLE_true

我想要做的是用分隔符分割字符串并存储到两个不同的数组中。下面是我如何遍历主数组:

String arrayA = [];
String arrayB = [];
for(int i = 0; i < selectedRecord.length; i++) {
   log.debug("HEY " + selectedRecord[i]);
   String tempRecord = selectedRecord[i];
}

我想要的输出是:

arrayA: 1564095_SINGLE, 1564096_SINGLE
arrayB: true, true

但我不知道如何拆分它。有任何想法吗?谢谢!

【问题讨论】:

  • String arrayA = []?这不是有效的 Java 代码。

标签: java delimiter


【解决方案1】:

这是一种在以下正则表达式模式下拆分输入的方法:

_(?!.*_)

这将仅在 last 下划线字符处拆分输入字符串。我们可以尝试迭代您的输入集合,然后填充两个数组。

List<String> inputs = Arrays.asList(new String[] {"1564095_SINGLE_true", "1564096_SINGLE_true"});
String[] arrayA = new String[2];
String[] arrayB = new String[2];
int index = 0;
for (String input : inputs) {
    arrayA[index] = input.split("_(?!.*_)")[0];
    arrayB[index] = input.split("_(?!.*_)")[1];
    ++index;
}
System.out.println("A[]: " + Arrays.toString(arrayA));
System.out.println("B[]: " + Arrays.toString(arrayB));

打印出来:

A[]: [1564095_SINGLE, 1564096_SINGLE]
B[]: [true, true]

【讨论】:

  • 您好,可以与您核实一下 new String[2] 是什么意思?
  • @BlackMamba new String[2] 是用于创建大小为 2 的字符串数组的语句的 RHS。
  • 我明白了,非常感谢!但是让我们说如果我没有数组的大小?我应该在那里指定什么
  • ...那么不要使用数组,而是使用列表:-)
  • 嗯,但如果我将它切换到 List,以下部分将出现问题 arrayA[index] = input.split("(?!.*)")[0 ];
【解决方案2】:

这有帮助吗?假设您可以应用基本检查(null、数组长度等)

  String[] selectedRecord = {"1564095_SINGLE_true", "1564096_SINGLE_true"};
    String[] arrayA = new String[selectedRecord.length];
    String[] arrayB = new String[selectedRecord.length];
    for (int i = 0; i < selectedRecord.length; i++) {
        arrayA[i] = selectedRecord[i].substring(0, selectedRecord[i].lastIndexOf("_"));
        arrayB[i] = selectedRecord[i].substring(selectedRecord[i].lastIndexOf("_")+1);
    }
    System.out.println(Arrays.asList(arrayA));
    System.out.println(Arrays.asList(arrayB));

【讨论】:

    【解决方案3】:

    检查下面的代码

    import java.util.*;
    public class MyClass {
    public static void main(String args[]) {
      
      String str="1564095_SINGLE_true, 1564096_SINGLE_true";
      System.out.println(str);
      String arr[]=str.split(",");
      ArrayList<String> arr1=new ArrayList();
       ArrayList<String> arr2=new ArrayList();
      
      for(int i=0;i<arr.length;i++)
      {
         String temp[]= arr[i].split("_");
       
         for(int j=0;j<temp.length;j++)
         {
            
             
             
           
                if(j==2)
                 {
                     arr2.add(temp[j]);
                 }
                 else
                {
                     arr1.add(temp[j]);
                }
             
             
         }
         
      }
      System.out.println(arr1);
      System.out.println(arr2);
    }
    

    }

    【讨论】:

      【解决方案4】:
          String selectedRecord[] = { "1564095_SINGLE_true", "1564096_SINGLE_true" };
          String[] arrayA = new String[selectedRecord.length];
          String[] arrayB = new String[selectedRecord.length];
          for(int i = 0; i < selectedRecord.length; i++) {
             String tempRecord = selectedRecord[i];
             int size = tempRecord.split("_").length;
             arrayB[i]= tempRecord.split("_")[size-1];
             arrayA[i]= tempRecord.replace("_"+arrayB[i], "");
          }
      
          System.out.println("ArrayA: "+ Arrays.asList(arrayA));
          System.out.println("ArrayB: "+ Arrays.asList(arrayB));
      

      输出:
      阵列A:[1564095_SINGLE,1564096_SINGLE]
      ArrayB:[真,真]

      【讨论】:

        【解决方案5】:

        您好,您可以这样做。获取分隔符的最后一个索引并将字符串子串化。

        String arrayA = [];
        String arrayB = [];
        for(int i = 0; i < selectedRecord.length; i++) {
           int end = selectedRecord.lastIndexOf("_");
           arrayA[i] = selectedRecord.substring(0, end);
           arrayB[i] = selectedRecord.substring(end+1);
        }
        

        当然这里应该是一些数据类型的转换。如果你想在布尔数组中存储“true”/“false”。

        【讨论】:

        • int end = selectedRecord.indexOf("_");这应该是 lastIndexOf。
        猜你喜欢
        • 1970-01-01
        • 2012-12-01
        • 2023-03-20
        • 1970-01-01
        • 2016-12-18
        • 2012-09-04
        • 1970-01-01
        • 2015-10-21
        • 1970-01-01
        相关资源
        最近更新 更多