Jump to content

  • Log In with Google      Sign In   
  • Create Account


How would you concatenate an array of strings together?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 ysg   Members   -  Reputation: 192

Like
0Likes
Like

Posted 10 May 2013 - 09:53 PM

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?

Sponsor:

#2 unbird   Crossbones+   -  Reputation: 4008

Like
2Likes
Like

Posted 10 May 2013 - 10:09 PM

I do!

#3 Nypyren   Crossbones+   -  Reputation: 3415

Like
2Likes
Like

Posted 11 May 2013 - 12:20 AM

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, 11 May 2013 - 12:25 AM.


#4 Endemoniada   Members   -  Reputation: 270

Like
-4Likes
Like

Posted 11 May 2013 - 07:04 PM

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

 

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



#5 Ectara   Crossbones+   -  Reputation: 2394

Like
6Likes
Like

Posted 11 May 2013 - 10:20 PM

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

 

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.


Edited by Ectara, 11 May 2013 - 11:33 PM.


#6 Khatharr   Crossbones+   -  Reputation: 2614

Like
0Likes
Like

Posted 13 May 2013 - 07:25 PM

Square knots are one good method for... Oh, wait.

 

No, unbird and Nypyren got it.


void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#7 Endemoniada   Members   -  Reputation: 270

Like
2Likes
Like

Posted 17 May 2013 - 03:53 PM

 

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

 

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS