Difference between revisions of "Concatenating arrays"

From CompSciWiki
Jump to: navigation, search
m (Fixed photo)
(Changed to utilize CodeBlock and OutputBlock templates)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{1010PrAD|ProblemName=Create a Grocery List
 
{{1010PrAD|ProblemName=Create a Grocery List
  
|Problem=Given an array of strings join the strings together to create a single string.
+
|Problem=Join an array of strings together to create a single string.
  
Example Output<br>
+
Example:<br>
<pre>
+
{{CodeBlock
String[] strings = { "First string is short.",  
+
|Code=
                     "Second string is a bit longer",  
+
String[] strings = { "First ",  
                     "Third sting is even more longererer" };
+
                     "Second ",  
</pre>
+
                     "Third" };
 +
}}
 +
Would have output:
 +
{{OutputBlock
 +
|Code=
 +
"First Second Third"
 +
}}
 
|SideSectionTitle=More with Arrays
 
|SideSectionTitle=More with Arrays
 
|SideSection=
 
|SideSection=
[[Image:Wiki_method01.jpg|center]]
+
[[Image:Wiki_method01.jpg|center]]<BR>
 
+
  
 
|Solution=
 
|Solution=
  
There are multiple ways of joining the strings together. The simplest way, using +,  you are probably already familiar with.
+
There are multiple ways of joining strings together. The simplest way is to use the plus symbol, which you are probably familiar with.
<pre>
+
{{CodeBlock
String[] strings = { "First string is short.", "Second string is a bit longer", "Third sting is even more longererer" };
+
|Code=
 
+
String combinedStrings = new String;
+
 
for( int i = 0; i < strings.length; i++ )
 
for( int i = 0; i < strings.length; i++ )
 
{
 
{
combinedStrings = combinedStrings + strings[i];
+
tempString1 = tempString1 + strings[i];
 
}
 
}
</pre>
+
System.out.println( "1. " + tempString1 );
 +
}}
  
  
 
+
The String class also offers the method concat() to concatenate strings.
The String class also offer the method, concat, to concatenate strings
+
{{CodeBlock
<pre>
+
|Code=
String[] strings = { "First string is short.", "Second string is a bit longer", "Third sting is even more longererer" };
+
 
+
String combinedStrings = new String;
+
 
for( int i = 0; i < strings.length; i++ )
 
for( int i = 0; i < strings.length; i++ )
 
{
 
{
combinedStrings.concat( strings[i] );
+
tempString2 = tempString2.concat( strings[i] );
 
}
 
}
</pre>
+
System.out.println( "2. " + tempString2 );
 +
}}
  
  
 
+
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
+
{{CodeBlock
<pre>
+
|Code=
String[] strings = { "First string is short.", "Second string is a bit longer", "Third sting is even more longererer" };
+
 
+
StringBuilder sb = new StringBuilder();
+
 
+
 
for( int i = 0; i < strings.length; i++ )
 
for( int i = 0; i < strings.length; i++ )
 
{
 
{
sb.append( strings[i] );
+
sb.append( strings[i] );
 
}
 
}
 +
tempString3 = sb.toString();
 +
System.out.println( "3. " + tempString3 );
  
String combinedStrings = sb.toString();
+
}}
  
</pre>
 
  
  
 +
|SolutionCode=
  
 +
public class stringTests
 +
{
 +
public static void main(String[] args)
 +
{
 +
String tempString1 = new String();
 +
String tempString2 = new String();
 +
String tempString3 = new String();
 +
 +
String[] strings = { "First ",
 +
                                    "Second ",
 +
                                    "Third" };
 +
 +
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 );
 +
 +
StringBuilder sb = new StringBuilder();
 +
 +
for( int i = 0; i < strings.length; i++ )
 +
{
 +
sb.append( strings[i] );
 +
}
 +
tempString3 = sb.toString();
 +
System.out.println( "3. " + tempString3 );
  
 
+
}
 
+
 
+
 
+
|SolutionCode=<pre>
+
public class HelloWorld
+
{
+
 
}
 
}
</pre>
 
  
 
}}
 
}}

Latest revision as of 16:31, 4 December 2011

Back to the Program-A-Day homepage

Problem

Join 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

Wiki method01.jpg

Solution

There 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 ); 


The String class also offers the method concat() to concatenate strings.

 for( int i = 0; i < strings.length; i++ )
{
	tempString2 = tempString2.concat( strings[i] );
}
System.out.println( "2. " + tempString2 ); 


Java also offers the StringBuilder class which is best used when building up larger strings.

 for( int i = 0; i < strings.length; i++ )
{
	sb.append( strings[i] );
}
tempString3 = sb.toString();
System.out.println( "3. " + tempString3 ); 

Code

Solution Code

Back to the Program-A-Day homepage