0

I have the following dates:

"Friday, January 31",
"Wednesday, February 12",
"Monday, February 17",
"Wednesday, March 5",

I want to set up a string function where I am given the number always:

31
12
17
5

I started with this function:

String strCheck = suspendedDates[i];
int pos = strCheck.length();
int pos2 = strCheck.indexOf(" ");

I am stuck right now, because how does it know which " " is it?

Can someone help me with the function.

1
  • It will take the first, as the documentation states. "Returns the index within this string of the first occurrence of the specified substring." Commented Jan 6, 2014 at 16:24

6 Answers 6

4

use lastIndexOf() instead of indexOf()

final String str = "Friday, January 31";
System.out.println(str.substring(str.lastIndexOf(" ")));
Sign up to request clarification or add additional context in comments.

Comments

2

As an alternative of @JigarJoshi answer you can use a regex if you don't mind, removing all non-number characters.

String result = dateString.replaceAll("[^\\d]+","");

2 Comments

Too much code + a regex is a heavy thing. If he only needs to get the last number and the format is the same he should use: Too much code. Just use s.substring(c.lastIndexOf(" "), s.length())
@DanielNuriyev I don't know if the format is always the same, but for the other thing you are right that is why i start my answer with "as an alternative" ;) im not gonna copy paste what other already answer
2

I had to post it; since this is a date I would go with Date parse to allow an additional check on the format of the inputs:

An alternative way:

SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM DD");
Calendar cal = Calendar.getInstance();

cal.setTime(format.parse("Friday, January 31"));
System.out.println(new SimpleDateFormat("DD").format(cal.getTime()));

2nd SimpleDateFormat("DD") is used instead of deprecated getDate().

2 Comments

+1 if the format is always the same this is more oop solution using SimpleFormatDate,
@nachokk thanks, and yes I could, there's always a scope I guess :-)
2

Get the substring from the end. Instead of trying to figure out locations

strCheck.substring(strCheck.length()-2);

This will take the last two characters. Then just do a trim() in case it's a single character to remove the space:-

strCheck.substring(strCheck.length()-2).trim();

Alternative

The other option as mentioned is to do a lastIndexOf() on the String with a space (" ") as an argument which will search backward from the end of the String till it finds the space. But as the number can always be extracted in 2 character spaces, I see no reason to do 2 character compares every time you want to extract a known size String (2) in a known location (length()-2) in order to retrieve the location that you already know.

3 Comments

There is no need to modify a string if he only needs to look up the last number. If the format is constant just do: Too much code. Just use s.substring(c.lastIndexOf(" "), s.length())
My code goes to the index given and extracts the desired String, yours will search backward from the end of the String doing compares until it finds a " ". Without the optional trim() my code is actually more efficient.
Look at the amount of code inside trim, including unnecessary code in this case. But it is quite possible that the difference is not significant. Let's run it a million times.
1

If you are trying to divide the string into parts for parsing, i suggest using String.split(" "), look at the javadocs and the internet for lots of nice examples of this in use! :)

Comments

1

You can use something like this:

 String str = "Friday, January 31";
 Scanner s = new Scanner(s);
 s.useDelimiter( "\\D+" );
 while ( s.hasNextInt() ){
   s.nextInt(); // get int
 }

2 Comments

Too much code. Just use s.substring(c.lastIndexOf(" "), s.length())
but more effective if you later want to extract more information or the format changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.