HOW TO SPLIT A STRING IN JAVA?

Ashish Pandey

Total Post:128

Points:898
Posted by  Ashish Pandey
Java  OOPS 
 1241  View(s)
Ratings:
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?


  1. Kamlakar Singh

    Post:194

    Points:1396
    Re: How to split a string in java?

    You can use spli method:

    String string = "004-034556";
    String[] parts = string.split("-");
    String part1 = parts[0]; // 004
    String part2 = parts[1]; // 034556

      Modified On Apr-05-2018 11:34:04 PM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!