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.


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

Results 1 to 3 of 3

Thread: Substring help

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Thanked 0 Times in 0 Posts

    Default Substring help

    Hello to all... this is my first post on the forum, and I'm sure I'll stick around for extra help

    Anyways. I'm practicing on CodingBat.com and got stuck on this task: CodingBat Java String-1 nTwice

    Here's my code:
    public String nTwice(String str, int n) 
      String a = str.substring(0,n);
      String b = str.substring();
      String c = a+b;
      return c;
    I THINK that my method will work, but I am unsure about how to define String b so that the program does what it is intended to do. What should go between the parentheses?
    Last edited by helloworld922; September 21st, 2010 at 08:51 AM.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 832 Times in 772 Posts
    Blog Entries

    Default Re: Substring help

    Read the API for String , more specifically the length method.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Thanked 329 Times in 305 Posts

    Default Re: Substring help

    Have a look at one of the substring methods. There is one with 1 parameter instead of two.

    This method will get the String:
    Starting from the the parameter index value AND
    Getting the rest of the String.

    So, while this method:
    String test = "test";
    String sub = test.substring(0,2);
    will set sub to be te,
    This method:
    String test = "test";
    String sub = test.substring(1);
    will set sub to be est.

    Does that make sense?

Similar Threads

  1. [SOLVED] replacing subString
    By nasi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 21st, 2010, 09:47 PM
  2. Substring function
    By bristol580 in forum Java SE APIs
    Replies: 2
    Last Post: November 12th, 2009, 11:29 AM
  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. Substring program, not working
    By Newoor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2009, 12:46 PM
  5. Replies: 5
    Last Post: January 30th, 2009, 09:31 PM