Java 使用定界符拆分字符串并存储到不同的数组

Java splitting string using delimiter and store to different array

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

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

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

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

_(?!.*_)

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

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]

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

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”。

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

  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));

检查下面的代码

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);
}

}

    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:[真,真]