2

I have a String that looks like this

The#red#studio#502#4

I need to split it into 3 different Strings in the array to be

s[0] = "The red studio"
s[1] = "502"
s[2] = "4"

The problem is the first one should have only words and the second and third should have only numbers...

I was trying to play with the s.split() Method, but no luck.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Andrey Chasovski
  • 145
  • 2
  • 2
  • 8
  • have you checked out javadocs for string.split() method ... – WeloSefer Jan 18 '13 at 03:48
  • Have you tried StringTokenizer .http://docs.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html – SANN3 Jan 18 '13 at 03:50
  • 5
    You need to understand the specific rules governing the input data. Is it _always_ `Some#text#with#words#number#number`? – Jim Garrison Jan 18 '13 at 03:51
  • Yes, it is always some text(unknown number of words) and then #number#number – Andrey Chasovski Jan 18 '13 at 03:54
  • 2
    What _exactly_ went wrong while using `s.split`? – Swapnil Jan 18 '13 at 03:54
  • 1
    [Lookahead](http://www.regular-expressions.info/lookaround.html#lookahead) mechanism might be useful in your case, although it will give you only split like `The#red#studio` `502` `4` so you will have to replace `#` in first part. – Pshemo Jan 18 '13 at 03:57

3 Answers3

10
String s= "The#red#studio#502#4";
String[] array = s.split("#(?=[0-9])");
for(String str : array)
{
  System.out.println(str.replace('#',' '));
}

Output:

The red studio  
502  
4  

Ideone link.

Srinivas
  • 1,780
  • 1
  • 14
  • 27
  • Can you explain that regex? Or at least tell us what that term is called so I can google it. Thanks – Daniel Kaplan Jan 18 '13 at 04:16
  • 1
    I knew the keywords 'Lookahead' and 'Lookbehind'. Quick search gave me [this](http://stackoverflow.com/a/2206432/189608). Basically, I split when there is a hash character and look for a decimal but I don't split on it, but rather split only on `#` – Srinivas Jan 18 '13 at 04:19
1

I've decided to edit out my impl because I think that @Srinivas's is more elegant. I'm leaving the rest of my answer though because the tests are still useful. It passes on @Srinivas's example too.

package com.sandbox;

import com.google.common.base.Joiner;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class SandboxTest {

    @Test
    public void testQuestionInput() {
        String[] s = makeResult("The#red#studio#502#4");
        assertEquals(s[0], "The red studio");
        assertEquals(s[1], "502");
        assertEquals(s[2], "4");
    }

    @Test
    public void testAdditionalRequirement() {
        String[] s = makeResult("The#red#studio#has#more#words#502#4");
        assertEquals(s[0], "The red studio has more words");
        assertEquals(s[1], "502");
        assertEquals(s[2], "4");
    }

    private String[] makeResult(String input) {
        // impl inside
    }
}
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
0

Simply try: 'String s[]= yourString.split("#")' it will return string array....

Kanagaraj M
  • 956
  • 8
  • 18