I work on C#. I have an array. To separate the array items I need to use comma. I did it but I think it's not efficient. How to do that, without an if condition? Please don't use replace method. My syntax is below.
string container = "";
string[] s = "Hellow world how are you".Split(' ');
foreach (string item in s)
{
if (container == "")
{
container += item;
}
else
{
container += "," + item;
}
}
I must need to c开发者_运维百科ontinue the loop. I just want below type solution.
string container = "";
string[] s = "Hellow world how are you".Split(' ');
foreach (string item in s)
{
container += "," + item;
}
Thanks in advance. If have any queries please ask.
Using String.Join
to join an array with comma separators.
string[] s = "Hello world how are you".Split(' ');
string container = String.Join(",", s);
Also, if you like getting help on this site, I recommend you start accepting a few answers.
Your problem is not the if
statement. Your problem is that it is generally poor form and bad practice to perform string concatenation and other manipulations in a loop. The string
class is immutable, changes are creating new strings, allocation new memory, etc. As a result, this practice is slow and inefficient, much more than your if
statement will be. The more iterations of your loop, the more you'll notice the inefficiency.
You should familiarize yourself with the StringBuilder
class, which allows you to perform efficient manipulations of a string
without repeatedly allocating new objects. It is particularly useful in loops like yours above.
An example of using a StringBuilder
is like the following
StringBuilder builder = new StringBuilder();
foreach (string item in array)
{
if (builder.Length != 0) builder.Append(",");
builder.Append(item);
}
string finalOutput = builder.ToString();
With that said, string.Join
is also a powerful tool for the type of concatenation you are performing.
string.split
starts with a string and ends with an array of strings and
is very fast . It uses unsafe code to determine the indexes of the separators. Then it allocates the array of the correct size and then cuts up the original string by allocating a bunch of other strings.
string.join
starts with an array of strings and ends with a string and is also very fast and uses unsafe code. It creates a buffer and adds to the buffer each item in the string growing the string as it goes.
But since you want to Start with a string and end with a string your best bet is to use a method that uses unsafe code to change the ' ' with ','.
string s1 = "Hellow world how are you";
fixed (char* p = s1)
{
for (int i = 0; i < s1.Length; i++)
{
if (p[i] == ' ')
{
p[i] = ',';
}
}
}
This is a really bad idea
- It only works because the source and target are the same length
- It requires unsafe code
- Since I'm mutating the string directly all references to the string get updated
- There's probably a bunch of checks that I'm missing
- Its only marginally faster then
string.replace
Just use String.Replace
if you really need it to be very fast and its very safe
精彩评论