Results 1 to 7 of 7
  1. #1
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Question How to Use substring and .concat

    Hello. I am new to Java and this forum. I apologize if this post is going to the wrong forum.
    I would like to concatenate strings Hi and Hello World.
    This is what I have:
    // Declare variables
    String sentence1;
    String sentence2;

    //Assign variables
    sentence1 = "Hi. ";
    sentence2 = "Hello World";
    System.out.println("The index of first w in sentence2 = " + sentence2.indexOf('o'));
    System.out.println("Concatenate sentence1 and sentence2: " + sentence1.concat(sentence2));

    This give me HIHello World.
    How do I put a space between Hi and Hello? I would like to use the substring method/function.
    Thanks In Advance

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    why do you need substrings?

    sentence1+=" "+sentence2;

  3. #3
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default How to Use substring and .concat

    Thank you for responding al_Marshy_1981. I had that statement in my code & was happy with the results. Then I re-read the instructions that said "concatenate the variable containing "Hi" with a substring of the variable containing "Hello World".

    And that's when my world went into a downward spiral. :-) I'm just not having any luck with substring.
    Thanks again.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You only need a substring of sentence2. So, you can use sentence2.substring(0,5) or something (1,6; 2,3; or anything like that, according to what you pasted) to pick a substring. Just be careful you don't make it say "Hi Hell". ;)

    Once you have the substring you can prepend sentence1 to it using the syntax that al Marshy provided.

  5. #5
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default How to Use substring and .concat

    Thank you al_Marshy_1981 & Zack. I'm doing fine now.
    Do I have to start a new post to ask a new question about dialogue boxes?

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    The general rule is that once a problem is outside the scope of a thread, it should have its own thread. So, in your case, yes.

  7. #7
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default (Solved) How to Use substring and .concat

    Thanks again.
    lala

Similar Threads

  1. substring Q
    By arsenal4ever_11 in forum NetBeans
    Replies: 1
    Last Post: 06-01-2010, 03:27 PM
  2. substring problem
    By mortoneff in forum New To Java
    Replies: 6
    Last Post: 08-19-2009, 05:38 PM
  3. Help with substring
    By hiklior in forum New To Java
    Replies: 12
    Last Post: 06-28-2008, 06:48 AM
  4. How can i get substring
    By 82rathi.angara in forum New To Java
    Replies: 14
    Last Post: 06-27-2008, 04:35 AM
  5. substring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •