How to split a string in Java
Asked 07 September, 2021
Viewed 1.1K times
  • 51
Votes

I have a string, "004-034556", that I want to split into two strings:

string1="004";
string2="034556";

That means the first string will contain the characters before '-', and the second string will contain the characters after '-'. I also want to check if the string has '-' in it. If not, I will throw an exception. How can I do this?

30 Answer