Skip to content

Instantly share code, notes, and snippets.

@cbilson
Created January 31, 2010 16:17
Show Gist options
  • Save cbilson/291131 to your computer and use it in GitHub Desktop.
Save cbilson/291131 to your computer and use it in GitHub Desktop.
// 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 = new char[] { ',', '\n' };
if (value.StartsWith("//"))
{
delimters = value.Substring(2, 1).ToCharArray();
value = value.Substring(4, value.Length - 4);
}
var values = value.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