A Function To Concat String Array Values By Specified Delimiter

Updated sandeepparekh9 -1 Tallied Votes 279 Views Share

A Function To Concat String Array Values By Specified Delimiter

Let 's Say I have Following Array:

string[] st = new string[5];
st[0] = "Animation";
st[1] = "Action";
st[2] = "Romance";
st[3] = "Drame";
st[4] = "Comedy";

Now I want to Merge all of it with ',' Delimiter Like Below:

Output : Animation,Action,Romance,Drame,Comedy

We will Call it Like This:
string str = GetAllStringsFromArrary( st,",");

Check here for better understanding: [snipped]

nick.crane commented: use string.Join!! -2
public string GetAllStringsFromArrary(string[] strArray,string strDelimeter)
        {
            string strFinal = string.Empty;
 
            for (int i = 0; i < strArray.Length ; i++)
            {
                strFinal += strArray[i];
 
                if (i != strArray.Length - 1)
                {
                    strFinal += strDelimeter;
                }
            }
            return strFinal;
             
 
        }
nick.crane 342 Veteran Poster

This function is provided by string.Join.
e.g. string.Join(", ", Values);

sandeepparekh9 109 Posting Whiz

ohhh.. wow. thanks.. i never realized it.. thanx again . :)

samueal 0 Junior Poster

Being a Posting Whiz, Have you never come across string.split and string.join inbuilt methods??..

sandeepparekh9 109 Posting Whiz

my first comment was a little sarcasm .. yes i know about string.split and string.join very well..

this was snippet was made very long ago.. i just posted it here to share ...

and the title Posting Wiz doesn't mean i know it all.. even the most talented people have flaws.. :)

debasisdas commented: polite enough. +13
maximocn 0 Newbie Poster

I was trying to apply what you've posted, thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.