Results 1 to 7 of 7
Thread: How to Use substring and .concat
- 10-26-2010, 05:33 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 80
- Rep Power
- 0
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
- 10-26-2010, 06:04 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
why do you need substrings?
sentence1+=" "+sentence2;
- 10-26-2010, 06:53 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 80
- Rep Power
- 0
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.
- 10-26-2010, 06:57 PM #4
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.
- 10-26-2010, 07:48 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 80
- Rep Power
- 0
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?
- 10-26-2010, 08:20 PM #6
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.
- 10-26-2010, 08:26 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 80
- Rep Power
- 0
Similar Threads
-
substring Q
By arsenal4ever_11 in forum NetBeansReplies: 1Last Post: 06-01-2010, 03:27 PM -
substring problem
By mortoneff in forum New To JavaReplies: 6Last Post: 08-19-2009, 05:38 PM -
Help with substring
By hiklior in forum New To JavaReplies: 12Last Post: 06-28-2008, 06:48 AM -
How can i get substring
By 82rathi.angara in forum New To JavaReplies: 14Last Post: 06-27-2008, 04:35 AM -
substring
By Java Tip in forum Java TipReplies: 0Last Post: 11-11-2007, 08:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks