Supposing we have a string that contains some text in the beggining and at the end of the string there is a url/link e.x. http://www.google.com . What is the best way to split this string in 2 variables : DescriptionTxt , LinkTxt
Thanks in advance.
Supposing we have a string that contains some text in the beggining and at the end of the string there is a url/link e.x. http://www.google.com . What is the best way to split this string in 2 variables : DescriptionTxt , LinkTxt
Thanks in advance.
String[] results = mystring.split(indexOf("http"));
Then if you wanted two separate Strings,
String DescriptionTxt = results[0];
String LinkTxt = results[1];
Detecting patterns is always tricky. There might be URLs that contain the keywords you look for. For instance:
A short description http://my.foo.bar/http-is-a-protocol
If you go with lastIndexOf("http"), your parser will fail. A good solution can be much more complex that assumed in the first place. In an advanced algorithm you could go for http://
, but https://
is just as valid. And don't forget capital letters like HTTP://
.
And is there a reason why http://
would not occur in your description, as well?
You won't get a complete solution here for your problem. Try to cover most of the cases with moderate effort and make sure you know what to do when your algorithm fails for something you haven't expected.