-6

I have text data and I want to split into String and int arrays Java

String text = "one1two20three300four4000";

Desired output:

String str[] = "one","two","three","four";

int num[] = 1,20,300,4000;
user229044
  • 232,980
  • 40
  • 330
  • 338
OHM
  • 1
  • 1
  • 1
    Why `JavaScript` is tagged ? – Rayon Dec 29 '15 at 06:16
  • Welcome to SO. Please read the following document to learn how to write good, answerable questions: http://stackoverflow.com/help/how-to-ask – Sam Dec 29 '15 at 06:18
  • 2
    Possible duplicate of [How to split a string in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – Mosam Mehta Dec 29 '15 at 06:20
  • @MosamMehta - Not exactly. He cannot split it *directly*. He will have to do some preprocessing to prevent empty array values – TheLostMind Dec 29 '15 at 06:22
  • You should post some example code ([Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve) or a [Short, Self-Contained, Correct Example](http://sscce.org/)). Include what you've already tried and where exactly you're stuck. See more info at [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) Thanks! – Will Dec 29 '15 at 06:44

1 Answers1

5

This one will work for you in Java :

public static void main(String[] args) {
    String text = "one1two20three300four4000";
    String arr1[] = text.replaceFirst("^\\d+", "").split("\\d+");
    String arr2[] = text.replaceFirst("^\\D+", "").split("\\D+");
    System.out.println(Arrays.toString(arr1));
    System.out.println(Arrays.toString(arr2)); // parse each value as an in using Streams (preferably) or a loop.
}

O/P :

[one, two, three, four]
[1, 20, 300, 4000]

PS : In java 8 Use int[] arr2Int = Arrays.stream(arr2).mapToInt(Integer::parseInt).toArray(); to convert your String array to int array.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104