Jump to content
  • Advertisement
Sign in to follow this  
ysg

How would you concatenate an array of strings together?

This topic is 2039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, say you pass in an array of strings and want to concatenate them together into one mega-string. What would be the best way? I don't see how a loop can't possibly involved, no?

Share this post


Link to post
Share on other sites
Advertisement

If you need delimiters between each string (such as comma separating a list), there is string.Join.

 

If you want to run a function on each string before joining them, you can do:  string.Join(separator, yourArrayOrCollection.Select(x => Operation(x));

 

If you want any kind of fancier operation, a StringBuilder and a loop might be the best approach.

Edited by Nypyren

Share this post


Link to post
Share on other sites

char buffer[4096]; // adjust
buffer[0]='\0';
 
for(int i=0;i<array_size;i++){
strcat(buffer,string_array);
}

 

You can implement a two pass system as well, the first pass can determine how big the buffer must be.

Share this post


Link to post
Share on other sites

 

char buffer[4096]; // adjust
buffer[0]='\0';
 
for(int i=0;i<array_size;i++){
strcat(buffer,string_array);
}

 

You can implement a two pass system as well, the first pass can determine how big the buffer must be.

I want to provide constructive criticism to say why you are getting downvotes, because it is frustrating to receive a negative reputation with out explanation.

That is the wrong language; there is no strcat() that I'm aware of in C#, and character arrays are not what is being used. That's also one of the least efficient ways to concatenate a collection of strings, due to having to count the length of the buffer each time you concatenate the string, which takes longer and longer.

 

Thank you. I didn't see the C# icon until just now.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!