Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
 
have you checked out javadocs for string.split() method ... –  WeloSefer Jan 18 at 3:48
 
Have you tried StringTokenizer .docs.oracle.com/javase/1.4.2/docs/api/java/util/… –  SAN3 Jan 18 at 3: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 at 3:51
1  
What exactly went wrong while using s.split? –  Swapnil Jan 18 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. –  Pshemo Jan 18 at 3:57
show 1 more comments

4 Answers

up vote 7 down vote accepted
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.

share|improve this answer
 
Can you explain that regex? Or at least tell us what that term is called so I can google it. Thanks –  tieTYT Jan 18 at 4:16
1  
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 # –  Srinivas Jan 18 at 4:19

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

share|improve this answer

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
    }
}
share|improve this answer
 
Can it be made more concise by only using split? –  Srinivas Jan 18 at 4:05
 
It is using split –  tieTYT Jan 18 at 4:05
 
only using split –  Srinivas Jan 18 at 4:07
1  
I don't know how, but it seems that you do. Your example also passes my tests. –  tieTYT Jan 18 at 4:16

I dont undestand. Your pattern is fixed. You final array always has 3 elements, then why dont just do it manually

Btw, my 2-cent code :D:

String  a = "The#red#studio#502#4";
String d [] = {a.split("#[0-9]+#[0-9]+")[0].replace("#", " "), a.split("#")[(a.split("#").length - 2)], a.split("#")[(a.split("#").length - 1)]};
System.out.println(Arrays.toString(d));
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.