Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have C# coding which takes group of values...

So I declare string[] to take all values......

Now I need to change string[] into stringbuilder...

Is there any possibility to change stringbuilder.....


Can anyone pls give me a solution
Posted
Comments
kodeLogic 5-Jun-12 5:44am    
Do you want to add the values in string[] into a StringBuilder ??
Zoltán Zörgő 5-Jun-12 9:59am    
Really nice of you accepting two out of three of the same solution. :(

 
Share this answer
 
C#
StringBuilder sb = new StringBuilder();
string[] StringArray = new string [] {"one","two","three"};
foreach(string s in StringArray)
{
   sb.Append(s);
}
 
Share this answer
 
v4
Comments
codeBegin 5-Jun-12 5:45am    
My 5!
Zoltán Zörgő 5-Jun-12 5:50am    
Thanks
string[] is a collection of strings, StringBuilder is a single string, so converting is not possible without specifying how, since one is 2 dimensional and the other is 1 dimensional.

You can :
C#
string[] strings ;
StringBuilder sb = new StringBuilder();
foreach (string s in strings)
    sb.Append(s);
 
Share this answer
 
Comments
codeBegin 5-Jun-12 5:45am    
My 5!
Mehdi Gholam 5-Jun-12 5:48am    
Thanks!
Abhinav S 5-Jun-12 5:45am    
My 5. Same answer as mine. :)
Mehdi Gholam 5-Jun-12 5:49am    
Thanks Abhinav!
Espen Harlinn 5-Jun-12 8:10am    
5'ed!
Very simply (assuming myArray as the string array)
StringBuilder sb = new StringBuilder();
foreach (string str in myArray)
{
  sb.AppendLine(str );
}
 
Share this answer
 
Comments
codeBegin 5-Jun-12 5:45am    
My 5!
Abhinav S 5-Jun-12 6:07am    
Thanks.
Mehdi Gholam 5-Jun-12 5:48am    
5'ed :)
Abhinav S 5-Jun-12 6:07am    
Thank you.
Espen Harlinn 5-Jun-12 8:11am    
5'ed!
C#
string[] test = new string[] {"a","b","c","d" };
           StringBuilder sb = new StringBuilder();

           foreach (var s in test)
           {
               sb.Append(s);
           }

           Response.Write(sb.ToString());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900