Home > DeveloperSection > Forums > How to split a string in java?
Ashish Pandey

Total Post:128

Posted on    October-10-2014 1:50 AM

 Java Java  OOPS 

 1 Reply(s)
 687  View(s)
Rate this:
I have a string "004-034556" that I want to split into two strings:

String str1 = "004";

String str2 = "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?

Kamlakar Singh
Kamlakar Singh

Total Post:194

Posted on    October-10-2014 1:52 AM

You can use spli method:

String string = "004-034556";

String[] parts = string.split("-");

String part1 = parts[0]; // 004

String part2 = parts[1]; // 034556

Don't want to miss updates? Please click the below button!

Follow MindStick