-1

Trying to get numbers only from this string. "abcd10efgh20"

However, the code below returns me an array with numbers with an empty string at the beginning.

["", "10", "20"]

String[] nums = s.split("\\D+");
System.out.println(Arrays.toString(nums));

Expecting ["10", "20"]

[Edit]

My question here is why do I get an empty string at the beginning of my array ? NOT

  • How to use regex ?
  • Get a sequence of numbers from the string.
  • How to code better ?

Thanks!

1
  • 2
    Have you considered consulting the Javadoc? Commented Aug 10, 2019 at 2:19

4 Answers 4

2

The reason you are getting the leading empty string is because your delimiter (a series of non-digit characters) leads the string. It would be the same reason why:

String s = ",";
String[] splitResult = s.split(",");

would give splitResult to be ["",""]. Every time there is a delimiter, even if it is at the beginning or end of the string, it is splitting two tokens in the string. If the delimiter is at the beginning of the string, at the end of the string, or if there are two adjacent delimiters (which wouldn't happen in your case because of the greedy + quantifier, but would happen in the above case with String s = ",,";), then the split result will have empty strings.

An easy way to solve your problem is to filter out the empty strings. You know it can only appear at the front or back of your input string, so it is not really a problem. E.g.:

  • Check the first and last string in nums. If they are empty, remove them.
  • Use regex to search for the digits, not split by the non-digits.
  • Remove any leading/trailing non-digits before running your code.

I'm not too fluent in Java, but I'm sure there's probably some cleaner Java idiom to do this with the code you've provided.

Sign up to request clarification or add additional context in comments.

2 Comments

:) Thanks for your explanation. It was helpful.
@SoumyaKumar Glad I helped. Good job for making sure the answers address your specific question!
0

I would checkout the StringUtils library for this purpose: https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html

Assuming this is a school project and external libraries aren't an option, this code should work for you:

    String s = "abcd10efgh20";
    String[] nums = s.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
    List<String> realnums = new ArrayList<>();
    for (int i = 0; i < nums.length; i++) {
        if (nums[i].matches("-?\\d+(\\.\\d+)?"))
        {
            realnums.add(nums[i]);
        }
    }

realnums will contain your ArrayList of numbers.

Basically what you're doing is splitting the string when it finds a number in the first part, then looping over that array and using a regular expression to determine if each element of the array is a number or not. If it is, add it to the new realnums ArrayList. If not, do nothing with it.

Comments

0
String s = "abcd10efgh20dsd2323sdfsdfsdf";
s = s.replaceAll("[aA-zZ]+", ",");

if (s.charAt(0) == ',') { s = s.replaceFirst(",", ""); }
if (s.charAt(s.length()-1) == ',') { s = s.substring(0, s.length()-1); }

String[] nums = s.split(",");

System.out.println(s);
System.out.println(Arrays.toString(nums));

Comments

0
String str="abcd10efgh20asdasd30";
    str=str.replaceAll("\\D+", ",");
    System.out.println(str);
    if(str.startsWith(","))
    {
        str=str.replaceFirst(",","");
    }
    System.out.println(str);
    System.out.println(Arrays.toString(str.split(",")));

1 Comment

Thanks, Core Java solution will be appreciated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.