Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the substring between two "/"

Tags:

java

string

For this program the user is told to input a data in the form of "mm/dd/yyyy" and I'm trying to use the indexOf() method with the parameter of "/" to break the date string into three substrings.

I tried doing this:

String monthString = dateString.substring(0,dateString.indexOf("/"));
    String dayString = 
    dateString.substring(dateString.indexOf("/"),DateString.indexOf("/")+1)

Thank you. Edit. Thank you all for your responses, but my teacher said that i cannot use the split fucntion. He said I can solve this using just indexOf("/") and substring(). I'll need two calls to indexOf("/") and four calls to substring().

like image 992
DefyingGravity Avatar asked Feb 25 '26 06:02

DefyingGravity


1 Answers

String[] element = dateString.split("/");
String strDay = element[0];
String strMonth = element[1];
String strYear = element[2];

Use String#split to get an array of the parts of your input string per your specified delimiter.

like image 76
Dang Nguyen Avatar answered Feb 26 '26 22:02

Dang Nguyen