0

Ok this is what I have in real life:

array = inputText.split("\n");

        for (String each : array) {

            listOfArray.add(each.split(" "));

        }

Explanation: i have got an input text box on a xhtml page. This box will contain a few rows. Each row is like:

Name1 Surname1 Age1 Sex1 
Name2 Surname2 Age2 Sex2
Name3 Surname3 Age3 Sex3

Then I create an array, whose fields will contain each row. Then I want to create a list of array, whose fields will contain each field of first array. But i want that "Name1 Surname1" is a singular field.

Marco
  • 322
  • 1
  • 3
  • 15
  • 1
    Just concatenate again your array[0] + " " +array[1] – Zava Jul 07 '16 at 12:45
  • @ZakariaeMAHLA this is just a simple example. I want an implementation that fits every general case of this kind. yet, the command split will do the array for me. I do not create any array. – Marco Jul 07 '16 at 12:46
  • 2
    I think my solution fits every use case :). Give me a counterexample – Zava Jul 07 '16 at 12:47
  • You've edited your comment, what command? Can you put more details please? – Zava Jul 07 '16 at 13:00
  • @ZakariaeMAHLA is there a way to use the command split(String,index) to do the job? – Marco Jul 07 '16 at 13:03

3 Answers3

4

If you are looking for solution for this specific case where you don't want to split on first space then you can use:

String line = "Name1 Surname1 Age1 Sex1";
String[] arr = line.split("(?<=\\s\\S{1,100})\\s");

System.out.println(Arrays.toString(arr));

Output: [Name1 Surname1, Age1, Sex1]

split("(?<=\\s\\S{1,100})\\s") splits on space which has space and 1-100 non-whitespace characters before it. So since first space doesn't have any space before, it will be skipped in splitting process.
Also I used {1,100} instead of + because in Java look-behind needs to have obvious maximal length so we can't use + there (at least theoretically since there are bugs which allows it like: https://stackoverflow.com/a/16486373/1393766).

Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • I think that is what he searched for. – creativecreatorormaybenot Jul 07 '16 at 13:22
  • this code line seems to be the one for me.. and i've got it through. I'm going to implement it and see if it works. – Marco Jul 07 '16 at 14:36
  • though it gives me an error on the "?" which is 'dangling near index "0" ' – Marco Jul 07 '16 at 15:05
  • @Marco It looks like you tried to split like `split("?")`. `?` is regex metacharacter representing `{0,1}` quantifier, but that quantifier doesn't make sense without element which it should describe like `ax?` can match `a` and `ax`. If you want to turn off its special meaning escape it with ``\`` (in string literals you need to also escape it so you need to write it like `split("\\?")`). – Pshemo Jul 07 '16 at 16:18
1

A more general solution than @creativecreator's would be:

String[] array = start.split(" ");
String[] finalArray = Arrays.copyOfRange(array, 1, array.length);
finalArray[0] = array[0] + " " + finalArray[0];

i.e. avoiding having to do all of the assignment explicitly.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0
String str = "Jon Snow 26 NightsWatch"
String[] values = str.split(" ");
String[] finalValues = new String[3];
for(int j=0; j<values.length; j++)
    if(j == 2)
    {
        index++;
        finalValues[index] = values[j];
        index++;
    }
    else
    {
        finalValues[index] += values[j] + " ";
    }

P.S. : This code is to show OP just an idea how to proceed. Bugs are not fixed and left for readers to fix.

Azodious
  • 13,752
  • 1
  • 36
  • 71