So I have a string that is like this:
"Some text here?Some number here"
and I need to split those, I am using String.split("\\?")
, but if I have a string like this:
"This is a string with, comma?1234567"
I have it splitted in the comma(,
) too. And if I have this String:
"That´s a problem here?123456"
It also splits on ´
, So how can I fix this?
The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.
The method split() splits a String into multiple Strings given the delimiter that separates them. The returned object is an array which contains the split Strings. We can also pass a limit to the number of elements in the returned array.
To split a string by special characters, call the split() method on the string, passing it a regular expression that matches any of the special characters as a parameter. The method will split the string on each occurrence of a special character and return an array containing the results.
I am not seeing this behaviour: (nor would I expect to)
String s ="hello?1000";
String[] fields = s.split("\\?");
for (String field : fields) {
System.out.println(field);
}
yields:
hello
1000
Introducing a comma "," or an apostrophe "'" doesn't make any difference to the split:
String s ="he,llo?1000";
yields:
he,llo
1000
String s ="he'llo?1000";
yields:
he'llo
1000
The spilt also works fine if you have any spaces in your input string. I can only suggest that your regex is not what you think it is!
this is the solution: (EDIT: it's even simpler)
public static Pair<String,String> getSplittedByQuestionMark(String term){
String[] list=term.split("[?]");
return new Pair<String,String>(list[0],list[1]);
}
i tested it:
@Test
public void testGetSplittedByQuestionMark(){
ArrayList<String> terms=new ArrayList<String>();
ArrayList<Pair<String,String>> expected=new ArrayList<Pair<String,String>>();
terms.add("test?a");
terms.add("test?20");
terms.add("test, with comma?ab10");
expected.add(new Pair<String,String>("test","a"));
expected.add(new Pair<String,String>("test","20"));
expected.add(new Pair<String,String>("test, with comma","ab10"));
for(int i=0;i<terms.size();i++){
Pair<String,String> answer = StringStandardRegex.getSplittedByQuestionMark(terms.get(i));
assertTrue("answer="+answer.getFirst(),answer.getFirst().equals(expected.get(i).getFirst()));
assertTrue("answer="+answer.getSecond(),answer.getSecond().equals(expected.get(i).getSecond()));
}
}
[EDIT after remark below] I have added a test, Now I don;t see what's the problem, this works as well (and is even more simpel):
@Test
public void testGetSplittedByQuestionMarkNotUsingRegex(){
ArrayList<String> terms=new ArrayList<String>();
ArrayList<Pair<String,String>> expected=new ArrayList<Pair<String,String>>();
terms.add("test?a");
terms.add("test?20");
terms.add("test, with comma?ab10");
expected.add(new Pair<String,String>("test","a"));
expected.add(new Pair<String,String>("test","20"));
expected.add(new Pair<String,String>("test, with comma","ab10"));
for(int i=0;i<terms.size();i++){
String[] answer=terms.get(i).split("\\?");
assertTrue("answer="+answer[0],answer[0].equals(expected.get(i).getFirst()));
assertTrue("answer="+answer[1],answer[1].equals(expected.get(i).getSecond()));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With