Home
Register
About
Login
Categories
C#
(13)
VB.NET
(0)
F#
(0)
ASP.NET
(15)
JavaScript
(4)
C++
(0)
C
(0)
Python
(0)
Ruby
(0)
Rails
(0)
J#
(0)
SQL
(0)
VB Script
(0)
HTML
(0)
CSS
(2)
XML
(2)
XSLT
(0)
Unit Testing
(7)
Architecture
(2)
Ajax
(0)
Project Management
(0)
LINQ to SQL
(2)
LINQ
(0)
LINQ to XML
(1)
NHibernate
(0)
Active Record
(0)
Silverlight
(0)
ASP.NET MVC
(0)
Life
(0)
Book Reviews
(0)
WPF
(0)
Convert String Array to Separator String
by azamsharp on 7/24/2008 9:16:29 AM
Here is a small piece of code that converts a string array to a separated string. The separator can be a comma, space etc.
// create a string out of an array
private static string GetSeparatedStringFromArray(string[] source, string separator)
{
StringBuilder sb = new StringBuilder();
foreach (string str in source)
{
sb.Append(str);
sb.Append(separator);
}
return sb.ToString().TrimEnd(separator);
}
Refactor it!
I think this one is much better! Now, the separator is char instead of a string. You can go one step forward and provide char[] instead of a single char.
by azamsharp on 7/24/2008 9:22:26 AM
// create a string out of an array
private static string GetSeparatedStringFromArray(string[] source, char separator)
{
StringBuilder sb = new StringBuilder();
foreach (string str in source)
{
sb.Append(str);
sb.Append(separator);
}
return sb.ToString().TrimEnd(separator);
}
Am I missing something? What's wrong with the following?
by MrBretticus on 7/24/2008 11:45:13 PM
String.Join(",", new string[] { "Item 1", "Item 2" });
Nice MrBretticus, I completely forgot about the String.Join method. Thanks for reminding me!
by azamsharp on 7/25/2008 6:34:58 AM
String.Join(",", new string[] { "Item 1", "Item 2" });
Please log in to refactor the code!
Login