1

I want to pass a string array (separated by commas), then use a function to split the passed array by a comma, and add in a delimiter in place of the comma.

I will show you what I mean in further detail with some broken code:

String FirstData = "1";
String SecondData = "2" ;
String ThirdData = "3" ;
String FourthData = null;

FourthData = AddDelimiter(FirstData,SecondData,ThirdData);

public String AddDelimiter(String[] sData)        
{
    // foreach ","
    String OriginalData = null;

    // So, here ... I want to somehow split 'sData' by a ",". 
    // I know I can use the split function - which I'm having 
    // some trouble with - but I also believe there is some way
    // to use the 'foreach' function? I wish i could put together 
    // some more code here but I'm a VB6 guy, and the syntax here 
    // is killing me. Errors everywhere.

    return OriginalData;

}
2
  • I rolled back the edit with the answer. The preferred process here is to accept an answer (check the checkbox next to the answer which answers your question); it has implications on reputation and sorting. If none of the answers suit your needs, then add your own answer with the solution, and accept that as the accepted answer. This will not increase your reputation though. Commented May 1, 2011 at 20:06
  • It looks like svick's answer is the one you want to accept, but I'm not sure. If it is indeed the answer, please mark it as the accepted answer. Commented May 1, 2011 at 20:07

5 Answers 5

2

Syntax doesn't matter much here, you need to get to know the Base Class Library. Also, you want to join strings apparently, not split it:

var s = string.Join(",", arrayOFStrings);

Also, if you want to pass n string to a method like that, you need the params keyword:

public string Join( params string[] data) {
  return string.Join(",", data);
}
Sign up to request clarification or add additional context in comments.

Comments

1

To split:

string[] splitString = sData.Split(new char[] {','});

To join in new delimiter, pass in the array of strings to String.Join:

string colonString = String.Join(":", splitString);

I think you are better off using Replace, since all you want to do is replace one delimiter with another:

string differentDelimiter = sData.Replace(",", ":");

5 Comments

Typical mistake: The signature of Split is ...(params char[] foo), so Split(',') is fully sufficient
@flq, but it's not a mistake, it's just another way to write the same code.
Thank you everyone. I would mark your answer one "up" but I don't have sufficient reputation yet. I will do so shortly.
@flq : um that is for VB in C# you have to do as @manojlds did.
@Evan Click the "tick" below up and down arrows for voting
0

If you have several objects and you want to put them in an array, you can write:

string[] allData = new string[] { FirstData, SecondData, ThirdData };

you can then simply give that to the function:

FourthData = AddDelimiter(allData);

C# has a nice trick, if you add a params keyword to the function definition, you can treat it as if it's a function with any number of parameters:

public String AddDelimiter(params String[] sData) { … }
…
FourthData = AddDelimiter(FirstData, SecondData, ThirdData);

As for the actual implementation, the easiest way is to use string.Join():

public String AddDelimiter(String[] sData)        
{
    // you can use any other string instead of ":"
    return string.Join(":", sData); 
}

But if you wanted to build the result yourself (for example if you wanted to learn how to do it), you could do it using string concatenation (oneString + anotherString), or even better, using StringBuilder:

public String AddDelimiter(String[] sData)        
{
    StringBuilder result = new StringBuilder();
    bool first = true;

    foreach (string s in sData)
    {
        if (!first)
            result.Append(':');
        result.Append(s);
        first = false;
    }

    return result.ToString();
}

3 Comments

I am going to try and rewrite this function using the params method! I will write back here in approximately five minutes on my result.
Here is where I am so far... (Note the slight error on i < sdata[].getupperbound;) - I'm not really sure why there is an error here. public String AddDelimiter(params String[] sData) { String FinalData = null; for (int i = 0; i < sData[].GetUpperBound; i++) { FinalData = FinalData + "Custom Delimiter" + sData[i]; } return FinalData; }
To get a length of an array, use the Length property, e.g. sData.Length. Otherwise, this code should work, but it's going to be slower than using StringBuilder, because strings are immutable and you have to allocate new one on each cycle. Also, foreach might be more readable than for, but that's just a stylistic choice.
0

One version of the Split function takes an array of characters. Here is an example:

   string splitstuff = string.Split(sData[0],new char [] {','});

1 Comment

I'm not clear why you can't use string FinalData = string.Join("Custom Delimiter",sData);
0

If you don't need to perform any processing on the parts in between and just need to replace the delimiter, you could easily do so with the Replace method on the String class:

string newlyDelimited = oldString.Replace(',', ':');

For large strings, this will give you better performance, as you won't have to do a full pass through the string to break it apart and then do a pass through the parts to join them back together.

However, if you need to work with the individual parts (to recompose them into another form that does not resemble a simple replacement of the delimiter), then you would use the Split method on the String class to get an array of the delimited items and then plug those into the format you wish.

Of course, this means you have to have some sort of explicit knowledge about what each part of the delimited string means.

2 Comments

I'm sure that this code will work just great. Just out of curiosity though, how would you recommend me first splitting the array into the three parts? That way I could put it back together like: FinalString = FirstData + "CustomDelim" + SecondData + "CustomDelim" + ThirdData
@Evan: Added to my answer to indicate what to do when you want the parts to construct another string with a different format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.