Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to return sub string from String

  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default How to return sub string from String

    Hi all im trying to return a sub String from a String. I have a small String saved as "myText" that contains lots of characters but just two numbers. The numbers are inbetween two markers, "start" & "finish". so the String would read: hello,goodbye(start) 35 (finish) hello,goodbye.
    i need to return just the numbers (the characters inbetween the markers)

    the way I have tried to do this is as follows:

    return myText.substring(myText.lastIndexOf(start), myText.indexOf(finish));

    however when I run the code it returns goodbye 35.

    Can anyone see what im doing wrong?
    Last edited by humdinger; February 12th, 2010 at 07:28 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to return sub string from String

    I don't see why you are getting that particular returned string, but that aside the methods indexOf return the start location of the found string, so you should add on the length of the string to get the index position of the end of the string

  3. The Following User Says Thank You to copeg For This Useful Post:

    humdinger (February 16th, 2010)

Similar Threads

  1. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 09:33 AM
  2. String Vs StringBuffer
    By kalees in forum Java SE APIs
    Replies: 5
    Last Post: November 6th, 2009, 03:27 AM
  3. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM
  4. First string is a substring of another
    By mgutierrez19 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 21st, 2009, 10:35 PM
  5. string to double
    By wolfgar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 13th, 2009, 10:47 PM