Arpit Jain
Arpit Jain

Reputation: 39

Unable to remove leading spaces when doing String .split()

I have a String [81, 82, 83, 84, 85, 86, 87, 88, 89, 90] and I am trying to create String array out of this with all the values.

String[] array = postId.split("[\\]\\,\\[\\s]+"); 

I am getting the first element in the array as space, rest all the elements are fine. Therefore, I am getting 11 items in my array rather than 10.

When doing sysout iterating over array I am getting the following value. You can see I am getting the first value as empty/space.

0 index item in array = 
1 index item in array = 81
2 index item in array = 82
3 index item in array = 83
4 index item in array = 84
5 index item in array = 85
6 index item in array = 86
7 index item in array = 87
8 index item in array = 88
9 index item in array = 89
10 index item in array = 90

Upvotes: 1

Views: 73

Answers (4)

The fourth bird
The fourth bird

Reputation: 163297

To get the numbers for the exact format of the example string, you could use a pattern to first match the format, and capture the comma separated value in group 1.

Then use the value from group 1, and split on a comma followed by 1 or more horizontal whitespace chars.

\bString\h+\[([0-9]+(?:,\h+[0-9]+)*)\]

Regex demo | Java demo

For example

String regex = "\\bString\\h+\\[([0-9]+(?:,\\h+[0-9]+)*)\\]";
String string = "String [81, 82, 83, 84, 85, 86, 87, 88, 89, 90]";

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);

if (matcher.find()) {
    String[] array = matcher.group(1).split(",\\h+");
    System.out.println(Arrays.toString(array));
}

Output

[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]

Upvotes: 0

JustAnotherDeveloper
JustAnotherDeveloper

Reputation: 2256

You can use substring to get rid of the square brackets:

import java.util.*;
public class MyClass {
    public static void main(String args[]) {
      String postId = "[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]";
      String[] array = postId.substring(1, postId.length()-1).split("[\\]\\,\\[\\s]+"); 
      System.out.println(Arrays.toString(array));

    }
}

Upvotes: 2

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

I would use regular expression for this task.

public static String[] extractNumbers(String str) {
    Pattern pattern = Pattern.compile("\\d+");
    Matcher matcher = pattern.matcher(str);
    List<String> numbers = new ArrayList<>();

    while (matcher.find()) {
        numbers.add(matcher.group(0));
    }

    return numbers.toArray(String[]::new);
}

Upvotes: 2

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79075

First, replace [ and ] with "" and then split it using the regex, ,\\s*

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String str = "[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]";
        String arr[] = str.replace("[", "").replace("]", "").split(",\\s*");
        System.out.println(Arrays.toString(arr));
    }
}

Output:

[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]

Upvotes: 2

Related Questions