Skip to content

Instantly share code, notes, and snippets.

@cbilson
Created February 2, 2010 04:41
Show Gist options
  • Save cbilson/292386 to your computer and use it in GitHub Desktop.
Save cbilson/292386 to your computer and use it in GitHub Desktop.
char[] GetDelimeters(string s)
{
return HasCustomTokens(s) ?
s.Substring(2, 1).ToCharArray()
: new char[] { ',', '\n' };
}
string GetValueToParse(string s)
{
return HasCustomTokens(s) ?
s.Substring(4, s.Length - 4)
: s;
}
bool HasCustomTokens(string s)
{
return s.StartsWith("//");
}
// Adds the integers in a string like "1,2,3" = 6,
// or "//;\n1;2;3" = 6
public int Add(string value)
{
if (string.IsNullOrEmpty(value))
return 0;
var delimters = GetDelimeters(value);
var values = GetValueToParse(s).Split(delimters);
var sum = 0;
foreach (var item in values)
{
sum += int.Parse(item);
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment