Reputation: 145
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.
Upvotes: 2
Views: 15803
Reputation: 67504
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
}
}
Upvotes: 1
Reputation: 966
Simply try: 'String s[]= yourString.split("#")' it will return string array....
Upvotes: 0