Difference between revisions of "Concatenating arrays"
From CompSciWiki
(spacing consistency, grammar, punct.) |
|||
Line 1: | Line 1: | ||
{{1010PrAD|ProblemName=Create a Grocery List | {{1010PrAD|ProblemName=Create a Grocery List | ||
− | |Problem= | + | |Problem=Join an array of strings together to create a single string. |
− | Example | + | Example:<br> |
<pre> | <pre> | ||
− | String[] strings = { "First | + | String[] strings = { "First ", |
− | "Second | + | "Second ", |
− | "Third | + | "Third" }; |
+ | </pre> | ||
+ | Would have output: | ||
+ | <pre> | ||
+ | "First Second Third" | ||
</pre> | </pre> | ||
|SideSectionTitle=More with Arrays | |SideSectionTitle=More with Arrays | ||
Line 16: | Line 20: | ||
|Solution= | |Solution= | ||
− | There are multiple ways of joining | + | There are multiple ways of joining strings together. The simplest way is to use the plus symbol, which you are probably familiar with. |
<pre> | <pre> | ||
for( int i = 0; i < strings.length; i++ ) | for( int i = 0; i < strings.length; i++ ) | ||
Line 26: | Line 30: | ||
− | + | The String class also offers the method concat() to concatenate strings. | |
− | The String class also | + | |
<pre> | <pre> | ||
for( int i = 0; i < strings.length; i++ ) | for( int i = 0; i < strings.length; i++ ) | ||
Line 37: | Line 40: | ||
− | + | Java also offers the StringBuilder class which is best used when building up larger strings. | |
− | Java also offers the StringBuilder class which is best used when building up larger strings | + | |
<pre> | <pre> | ||
for( int i = 0; i < strings.length; i++ ) | for( int i = 0; i < strings.length; i++ ) | ||
Line 58: | Line 60: | ||
|SolutionCode= | |SolutionCode= | ||
− | public class stringTests | + | public class stringTests |
− | + | { | |
public static void main(String[] args) | public static void main(String[] args) | ||
{ | { | ||
Line 66: | Line 68: | ||
String tempString3 = new String(); | String tempString3 = new String(); | ||
− | String[] strings = { "First | + | String[] strings = { "First ", |
− | + | "Second ", | |
− | + | "Third" }; | |
for( int i = 0; i < strings.length; i++ ) | for( int i = 0; i < strings.length; i++ ) |
Revision as of 11:21, 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 |