Difference between revisions of "Concatenating arrays"
From CompSciWiki
(spacing consistency, grammar, punct.) |
m (Fixed photo) |
||
Line 15: | Line 15: | ||
|SideSectionTitle=More with Arrays | |SideSectionTitle=More with Arrays | ||
|SideSection= | |SideSection= | ||
− | [[Image:Wiki_method01.jpg|center]] | + | [[Image:Wiki_method01.jpg|center]]<BR> |
− | <BR> | + | |
|Solution= | |Solution= |
Revision as of 14:27, 9 April 2010
Back to the Program-A-Day homepage
ProblemJoin an array of strings together to create a single string. Example: String[] strings = { "First ", "Second ", "Third" }; Would have output: "First Second Third" |
More with Arrays | |
---|---|---|
SolutionThere are multiple ways of joining strings together. The simplest way is to use the plus symbol, which you are probably familiar with. for( int i = 0; i < strings.length; i++ ) { tempString1 = tempString1 + strings[i]; } System.out.println( "1. " + tempString1 );
for( int i = 0; i < strings.length; i++ ) { tempString2 = tempString2.concat( strings[i] ); } System.out.println( "2. " + tempString2 );
for( int i = 0; i < strings.length; i++ ) { sb.append( strings[i] ); } tempString3 = sb.toString(); System.out.println( "3. " + tempString3 ); | ||
Code |