0

I was having some problem when trying to split string with delimiter and store to array. So basically I have a main array with input like this:

1564095_SINGLE_true, 1564096_SINGLE_true

What I am trying to do is split the string with delimiter and store to two different array. Below as how I loop thru the main array:

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

My desired output will be:

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

But I have no idea on how to split it. Any ideas? Thanks!

1
  • 1
    String arrayA = []? That is not valid Java code. Commented Aug 3, 2021 at 7:29

5 Answers 5

2

Here is one approach which splits in the input on the following regex pattern:

_(?!.*_)

This splits the input string on only the last underscore character. We can try iterating your collection of inputs, and then populating the two arrays.

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

This prints:

A[]: [1564095_SINGLE, 1564096_SINGLE]
B[]: [true, true]
Sign up to request clarification or add additional context in comments.

7 Comments

Hi can check with you what does the new String[2] means?
@BlackMamba new String[2] is the RHS of the statement used to create a string array of size 2.
I see I see thanks so much! but lets say if I do not have the size of the array? what should I specify there
... then don't use arrays, use a list instead :-)
Hmm but if I switch it to List, the following part will hv problem arrayA[index] = input.split("(?!.*)")[0];
|
2

Does this help? Assuming you can apply basic checks (null, array length, etc)

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

Comments

0

check below code

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

}

Comments

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

Output:
ArrayA: [1564095_SINGLE, 1564096_SINGLE]
ArrayB: [true, true]

Comments

0

Hi you can do something like this. Get the last index of the delimiter and substring the string.

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

Of course here should be some datatype conversions. If you want to store "true"/"false" inside of the boolean array.

1 Comment

int end = selectedRecord.indexOf("_"); This should be lastIndexOf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.