954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

A Function To Concat String Array Values By Specified Delimiter

By sandeepparekh9 on Jun 2nd, 2011 2:38 pm

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]

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;
             
 
        }

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

nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
 

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

sandeepparekh9
Posting Whiz
367 posts since Dec 2010
Reputation Points: 123
Solved Threads: 71
 

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

samueal
Junior Poster
111 posts since Apr 2011
Reputation Points: 18
Solved Threads: 16
 

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.. :)

sandeepparekh9
Posting Whiz
367 posts since Dec 2010
Reputation Points: 123
Solved Threads: 71
 

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

maximocn
Newbie Poster
3 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You