I have the following string:
Mr John Smith Dickson <john@yahoo.com>
I want to split it into three parts:
1st part – Mr
2nd part – John Smith Dickson
3rd part – john@yahoo.com
I’m confused with how I might go about accomplishing this, can anyone help?
the above name is just sample, the name might be vary, eg. John, John Smith, John Smith Dickson
You should use a regex. You can capture the first word up until whitespace, then the next three words separated by whitespace, then the thing in the angle brackets.
this works
\w means any word character. The + means 1 or more. \s means any whitespace character. Things in () are captured. The regex you would use in java code is
tested here
note that you could do this with splits, but anytime you are splitting, then getting the tokens, then splitting tokens, then getting more tokens, then splitting again, you are doing something too complicated. Regex greatly simplifies things.