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: String class: using the substring method to obtain only the last character in the str

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Location
    norcross, ga
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question String class: using the substring method to obtain only the last character in the str

    Given a string, return a new string where the first and last chars have been exchanged.

    frontBack("code") → "eodc"
    frontBack("a") → "a"
    frontBack("ab") → "ba"

    [FONT="Courier New"]public String frontBack(String str)
    {
      String firstLetter = str.substring(0,1);
      String lastLetter = str.substring([B]str.length - 1[/B, str.length); //is that valid?..
    }[/FONT]


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: String class: using the substring method to obtain only the last character in the

    Use a substring on the middle section of the string, and just use charAt to get the starting/ending characters for swapping (note: this code needs to be modified so it will check to see if the string has one or less characters, otherwise it will fail)

    String flipped = str.charAt(str.length()) + str.subString(1,str.length() - 1) + str.charAt(0);

    Substring works like this:
    The first parameter is the index of the character to include. The second parameter is the index of the character to stop before (this character is not included).

    ex:
    String str = "0123456789";
    System.out.println(str.substring(2,4));  // will printout "23"

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

    etidd (February 4th, 2010)

Similar Threads

  1. Replies: 5
    Last Post: April 22nd, 2013, 07:27 AM
  2. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 12:13 PM
  3. 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
  4. how to reuse method returned string ??
    By zeeshanmirza in forum Collections and Generics
    Replies: 7
    Last Post: September 4th, 2009, 03:54 PM
  5. String substring, concatenation operation on linked list
    By oaks12 in forum Collections and Generics
    Replies: 1
    Last Post: January 15th, 2009, 07:12 AM