File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes substring?? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "substring??" Watch "substring??" New topic
Author

substring??

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
Consider the code..
String s = "MINIMUM";
1)System.out.println(s.substring(s.indexOf('I', 3)));
2)System.out.println(s.substring(s.indexOf('I', 4)));
Answer 1) "INI"
Answer 2) "INIM"
Am I right.?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Why don't you write yourself a little test program, compile it, and run it, and then you can see for yourself what the answer is!
Rob


Rob
SCJP 1.4
sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
Rob,
Actually this question was from jq+ and I was a bit confused because the answer for the first question was "MUM"
A little bit confused.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
The first one will return "IMUM" and the second throws a StringIndexOutOfBoundsException.
You should write yourslef a test program and run it and verify the results yourself.
Rob
patrick tang
Ranch Hand

Joined: Dec 16, 2001
Posts: 44
s.indexOf('I', 3) means searching 'I' starting from index 3. as the second 'I' is index 3, it will return 3.
s.indexOf('I', 4) will return -1 because there's no 'I' if searching from index 4. (remember index starts from 0).
therefore first one will print IMUM and second one will give a RuntimeException.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: substring??
 
Similar Threads
How to format the number
doubt in indexOf()
String to String array
strange checkbox problem
Strings urgent please