0

I have a string that is read in pairs, separated by comma. However, I do not always want to split at the comma because there is not always 1 comma in the input. For example, the string,

(http://www.wolframalpha.com/input/?i=103%2F30+%3D+4a-3b,+71%2F60+%3D+a+%2B+b
,http://www.wolframalpha.com/input/?i=x%5E2%2B5x%2B6,file:///tmp/foo/bar/p,d,f.pdf)

Is read in all one line. For this case, I only want to split at the ,h, and no where else in the string. Essentially, after the split, the strings should be:

http://www.wolframalpha.com/input/?i=103%2F30+%3D+4a-3b,+71%2F60+%3D+a+%2B+b

http://www.wolframalpha.com/input/?i=x%5E2%2B5x%2B6

file:///tmp/foo/bar/p,d,f.pdf

Maintaining the order of the comma in the first string. (I will get rid of parenthesis). I have looked at this stack overflow question, and while helpful, does not correctly split this string. This is in Java. Any help is appreciated.

Community
  • 1
  • 1
Nick
  • 823
  • 2
  • 10
  • 22

2 Answers2

5

You can use regex to do the split. Please see below code snippet.

String str = "(http://www.wolframalpha.com/input/?i=103%2F30+%3D+4a-3b,+71%2F60+%3D+a+%2B+b,http://www.wolframalpha.com/input/?i=x%5E2%2B5x%2B6)";
String[] strArr = str.split("(,(?=http))");

You will have Array of all the value which would be possible according to your requirement.

SachinSarawgi
  • 2,632
  • 20
  • 28
1

Split on 'http' then re-add it.

Psuedo-code

String input = "http://www.wolframalpha.com/input/?i=103%2F30+%3D+4a-3b,+71%2F60+%3D+a+%2B+b
,http://www.wolframalpha.com/input/?i=x%5E2%2B5x%2B6"

List<String> split = input.split('http');
List<String> finalList = new ArrayList<String>();

for(String fixup in split)
{
  finalList.put( "http" + fixup );
}

Final should contain the two URLs.

Freiheit
  • 8,408
  • 6
  • 59
  • 101
  • You might also have to trim off trailing commas – Freiheit Dec 13 '16 at 19:40
  • What if the 'http:' (the protocol part) is missing from the url? – Yoram Dec 13 '16 at 19:42
  • 1
    @Yoram - If the http part is missing then its not a URL. Based on the example given, I am assuming that this is a comma separated list of URLs which may include commas in the URL. If it is a comma separated list of something else which may include commas then a different solution will be needed. – Freiheit Dec 13 '16 at 19:43
  • 1
    @Freiheit there are some edge cases where the user opens up a PDF in the browser, so the string would be file:\\\. . . – Nick Dec 13 '16 at 19:46