Delimited string helper class in C#

Build delimited strings with less code!

Having to build a string with elements delimited with special characters for a CSV file, a HTTP GET request or any other purpose?
How many times did you made a loop to construct the string, having to user a flag to check if the delimiter have to be added, either at the beginning or the end or the string, to make sure chere is not an intrusive delimiter in the result? To make sure the string don't have the delimiter at the beginning or at the end?

This simple class uses a StringBuilder to construct a key / value string delimited with a given character or string just between elements.


using System;
using System.Text;


/// <summary>
/// To construct a string delimited only between elements.
///
/// Simple helper class to construct dynamically a delimited string
/// with the delimiter inserted automatically only between elements,
/// not at the beginnig nor at the end of the resulting string.
///
/// Uses a StringBuilder as the inner builder.
/// Denis Sauvé
/// </summary>
public class DelimitedStringBuilder
{
protected string delimiter = null;
protected StringBuilder sb = new StringBuilder();
// inner StringBuilder, as StringBuilder class cannot be inherited
/// <summary>
/// New instance of a DelimitedStringBuilder with delimiter between elements
/// </summary>
/// <param name="delimiter">delemiter to use (",", "\n", etc)</param>
public DelimitedStringBuilder(string delimiter)
{
this.delimiter = delimiter;
}
public void Append(string stringToAppend)
{
if (sb.Length >0)
if (delimiter != null) sb.Append(delimiter);
sb.Append(stringToAppend);
}
public override string ToString()
{
return sb.ToString();
}
public StringBuilder InnerStringBuilder
{
get
{
return this.sb;
}
}

}


Note: you have to make sure the delimiter is not in the keys nor in the values... But tThe Append() method could be modified easilly to check that.

5 comments:

Anonymous said...

viagra generic soft tab viagra prices how does viagra work viagra online uk non prescription viagra cheap generic viagra too much viagra viagra canada soma and viagra prescriptions free viagra uk alternative viagra splitting viagra viagra free pills viagra faq try viagra for free

Anonymous said...

[url=http://kfarbair.com][img]http://www.kfarbair.com/_images/_photos/photo_big8.jpg[/img][/url]

בית מלון [url=http://www.kfarbair.com]כפר בעיר[/url] - אינטימיות, [url=http://kfarbair.com/services.html]שקט[/url] . אנו מציעים שירותי אירוח מגוונים כמו כן יש במקום שירות חדרים הכולל [url=http://www.kfarbair.com/eng/index.html]סעודות רומנטיות[/url] במחירים מיוחדים אשר מוגשות ישירות לחדרכם!

לפרטים נוספים אנא גשו לאתרנו - [url=http://kfarbair.com]כפר בעיר[/url] [url=http://www.kfarbair.com/contact.html][img]http://www.kfarbair.com/_images/apixel.gif[/img][/url]

Anonymous said...

Online betting is just another rapidly growing diversion in the visual community. You will bet on the sport you choose through websites on the internet. All the details of betting are stated clearly on the site itself, including the possible amount that you may get if you'll win. [url=http://www.pulsebet.com/bg/]bet online[/url] [url=http://www.pulsebet.com/da/]bet online[/url] [url=http://www.pulsebet.com/de/]bet online[/url] [url=http://www.pulsebet.com/es]apuestas[/url] [url=http://www.pulsebet.com/fr/]jouer en ligne[/url] [url=http://www.pulsebet.com/gr]bet online[/url] [url=http://www.pulsebet.com/jp]bet online[/url] [url=http://www.pulsebet.com/nl]bet online[/url] [url=http://www.pulsebet.com/pl]bet online[/url] [url=http://www.pulsebet.com/pt]bet online[/url] [url=http://www.pulsebet.com/ro]bet online[/url] [url=http://www.pulsebet.com/ru]bet online[/url] [url=http://www.pulsebet.com/se]bet online[/url] [url=http://www.pulsebet.com/it]bet online[/url] [url=http://www.pulsebet.com/cn]bet online[/url] The line shows that San Diego is -3, with a total of 44-points. The reason why there is a line is because one team is going to be favored over another to win the game. This way the oddsmakers can level the playing field, making the bet attractive to both sides. In this case San Diego is the favorite and New England is the underdog. This means that if you are placing your bet on San Diego, the Chargers will have to beat the Patriots by at least 3 points for you to win the bet. If they lose the game or win by less than 3, you lose your bet. The second number shows 44, which is the over/under score. By betting the over, you are putting money on the possibility that the sum of both team’s score will be greater than 44-points at the end of the game. http://www.pulsebet.com/bg/ http://www.pulsebet.com/da/ http://www.pulsebet.com/de/ http://www.pulsebet.com/es/ http://www.pulsebet.com/fr/ http://www.pulsebet.com/gr/ http://www.pulsebet.com/jp/ http://www.pulsebet.com/nl/ http://www.pulsebet.com/pl/ http://www.pulsebet.com/pt/ http://www.pulsebet.com/ro/ http://www.pulsebet.com/ru/ http://www.pulsebet.com/se/ http://www.pulsebet.com/it/ http://www.pulsebet.com/cn/

Anonymous said...

buy xanax best kind xanax bars - xanax 1mg value

Anonymous said...

http://site.ru - [url=http://site.ru]site[/url] site
site