wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String .. substring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String .. substring" Watch "String .. substring" New topic
Author

String .. substring

Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
This question is a very basic one.. but the answer given int the Sun Guoqiao's mock exam got me confused
What is the output of trying to compile and run the following code?
(Select one correct answer)
-----------------------------------------------------------------------
public class Test023
{
public static void main(String args[])
{
String str = new String("Hello");
System.out.print(str.substring(0,3));
System.out.print(" ");
System.out.println(str.replace('r', 'h'));
}
}
-----------------------------------------------------------------------
A: Hell Hello
B: Hel Hel
C: Hel Hello
D: Hell Hell

My answer was A but the correct answer is C..
The API doc has the following example which made me believe that the string index starts from 0 & not 1.

"smiles".substring(1, 5) returns "mile"

Can anybody clear my confusion
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Roopa
The correct answer is C.
String str = new String("Hello");
This line assigns the String 'Hello' to the variable str
System.out.print(str.substring(0,3));
This line prints the substring from the index 0 to the index 3-1. The substring method returns a new string from the first index to 1 less then the second index. The string is 'Hel'.

System.out.print(" ");
This line prints the space.
System.out.println(str.replace('r', 'h'));
This returns a string that has had all of the occurances of 'r' replaced with an 'h'. Since there wheren't any r's it returns the original string which is 'Hello'.
So the final output is 'Hel Hello'.
Hope that helps


------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267

Thankyou Dave..
Originally posted by Dave Vick:
Roopa
The correct answer is C.
String str = new String("Hello");
This line assigns the String 'Hello' to the variable str
System.out.print(str.substring(0,3));
This line prints the substring from the index 0 to the index 3-1. The substring method returns a new string from the first index to 1 less then the second index. The string is 'Hel'.

System.out.print(" ");
This line prints the space.
System.out.println(str.replace('r', 'h'));
This returns a string that has had all of the occurances of 'r' replaced with an 'h'. Since there wheren't any r's it returns the original string which is 'Hello'.
So the final output is 'Hel Hello'.
Hope that helps


 
Consider Paul's rocket mass heater.
 
subject: String .. substring
 
Similar Threads
String a-go-go
String literals
String questions
doubt in Strings
substring confusion...