Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected behaviour when trying to use String.split("\\?")

Tags:

java

string

split

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?

like image 552
fredcrs Avatar asked Nov 11 '10 13:11

fredcrs


People also ask

What does split method does to a string explain with example?

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.

What is split () function in string?

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.

What happens when you split a string in Java?

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.

How do you break a string with special characters?

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.


2 Answers

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!

like image 147
Richard H Avatar answered Oct 22 '22 06:10

Richard H


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()));
    }

}
like image 34
michel.iamit Avatar answered Oct 22 '22 05:10

michel.iamit