Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-02-2008, 11:35 AM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
String substring function
Hi,

I am using the substring methodto get part of a string.

Code:
String string = "WelcomeToJava"; System.out.println(string.substring(9,12));
Output:
Code:
Jav
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-02-2008, 12:02 PM
simi's Avatar
Member
 
Join Date: Dec 2007
Location: Singapore
Posts: 18
simi is on a distinguished road
Send a message via Yahoo to simi
String string = "WelcomeToJava";
System.out.println(string.substring(9,13));
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-02-2008, 12:06 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
I hope java docs on substring method of String class should make you more clear on this.
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-02-2008, 12:45 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-02-2008, 01:18 PM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-02-2008, 07:54 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Quote:
Originally Posted by tim View Post
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
Very interesting Tim... I learned something new today, seriously.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-02-2008, 09:35 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Thank you CaptainMorgan.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to split a String using split function Java Tip java.lang 0 04-04-2008 04:39 PM
help me with a realy easy program (substring) michcio New To Java 7 01-27-2008 02:41 AM
Using java.util.Scanner to search for a String in a String Java Tip Java Tips 0 11-20-2007 06:59 PM
substring Java Tip Java Tips 0 11-11-2007 10:15 PM
How to split a String using split function JavaBean Java Tips 0 10-04-2007 11:32 PM


All times are GMT +3. The time now is 02:38 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org