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.

6
  • have you checked out javadocs for string.split() method ... Commented Jan 18, 2013 at 3:48
  • Have you tried StringTokenizer .docs.oracle.com/javase/1.4.2/docs/api/java/util/… Commented Jan 18, 2013 at 3:50
  • 5
    You need to understand the specific rules governing the input data. Is it always Some#text#with#words#number#number? Commented Jan 18, 2013 at 3:51
  • 2
    What exactly went wrong while using s.split? Commented Jan 18, 2013 at 3:54
  • 1
    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. Commented Jan 18, 2013 at 3:57

3 Answers 3

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.

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

2 Comments

Can you explain that regex? Or at least tell us what that term is called so I can google it. Thanks
I knew the keywords 'Lookahead' and 'Lookbehind'. Quick search gave me this. 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 #
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
    }
}

2 Comments

Can it be made more concise by only using split?
I don't know how, but it seems that you do. Your example also passes my tests.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.