Created
February 2, 2010 04:04
-
-
Save cbilson/292345 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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