Results 1 to 7 of 7
Thread: String substring function
- 01-02-2008, 09:35 AM #1
Senior Member
- Join Date
- Nov 2007
- Posts
- 115
- Rep Power
- 0
String substring function
Hi,
I am using the substring methodto get part of a string.
Output:Java Code:String string = "WelcomeToJava"; System.out.println(string.substring(9,12));
I want to get output "Java". The index of "J" is 9 and index of a is 12. It appears that the first argument to the method is inclusive and the second one is exclusive. Please correct me.Java Code:Jav
- 01-02-2008, 10:02 AM #2
String string = "WelcomeToJava";
System.out.println(string.substring(9,13));
- 01-02-2008, 10:06 AM #3
I hope java docs on substring method of String class should make you more clear on this.
dont worry newbie, we got you covered.
- 01-02-2008, 10:45 AM #4
Hello, ravian.
The substring method is a funny one. The way that I understand is that the first parameter has zero-based indexing and the second has one-based indexing. For example:
first parameter:____00 01 02 03 04 05 06 07 08 09 10 11 12
second parameter:___01 02 03 04 05 06 07 08 09 10 11 12 13
string:_____________W__e__l__c__o__m__e__T__o__J__a__v__a
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-02-2008, 11:18 AM #5
Senior Member
- Join Date
- Nov 2007
- Posts
- 115
- Rep Power
- 0
This explains. As tim said: the first parameter is zero based and the second one is one based.
Strange but its like this.
Thanks everyone.
- 01-02-2008, 05:54 PM #6
- 01-02-2008, 07:35 PM #7
Similar Threads
-
How to split a String using split function
By Java Tip in forum java.langReplies: 4Last Post: 04-17-2009, 08:27 PM -
help me with a realy easy program (substring)
By michcio in forum New To JavaReplies: 7Last Post: 01-27-2008, 12:41 AM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM -
substring
By Java Tip in forum Java TipReplies: 0Last Post: 11-11-2007, 08:15 PM -
How to split a String using split function
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks