Skip to content

Instantly share code, notes, and snippets.

@cbilson
Created February 2, 2010 04:04
Show Gist options
  • Save cbilson/292345 to your computer and use it in GitHub Desktop.
Save cbilson/292345 to your computer and use it in GitHub Desktop.
char[] GetDelimeters(string s)
{
if (s.StartsWith("//"))
return s.Substring(2, 1).ToCharArray();
else
return new char[] { ',', '\n' };
}
string GetValueToParse(string s)
{
if (s.StartsWith("//"))
return s.Substring(4, s.Length - 4);
else
return s;
}
// 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