I have string "1999220" how to parse?
-
I have develop simple application first I take birthday from user and calculate his lucky number from sum of the values of the birthday Ex: '19881205' How i get the sum of values in this string?
Use one of the integer methods[^] to parse it into a number and then use the Digital root method[^]. Alternatively extract each character one at a time, and sum the integer values.
Use the best guess
-
I have develop simple application first I take birthday from user and calculate his lucky number from sum of the values of the birthday Ex: '19881205' How i get the sum of values in this string?
Long winded, but as a demo...
static void SumNumbersinDate()
{
//http://www.codeproject.com/Messages/4513781/I-have-string-1999220-how-to-parse.aspx
string theDate = "19881205";
char[] dateLetters = theDate.ToCharArray();
int[] dateNumbers = new int[theDate.Length];
int sum = 0;
for (int ind = 0; ind < theDate.Length; ind++)
{
Console.WriteLine("Current Total: " + sum.ToString());
dateNumbers[ind] = int.Parse(dateLetters[ind].ToString());
Console.WriteLine("Adding: " + dateNumbers[ind].ToString());
sum += dateNumbers[ind];
Console.WriteLine("New Total: " + sum.ToString());
}
Console.WriteLine("The sum of the numbers is: " + sum.ToString());
Console.WriteLine("Press any key to close the demo...");
Console.ReadKey(true);
} -
Use one of the integer methods[^] to parse it into a number and then use the Digital root method[^]. Alternatively extract each character one at a time, and sum the integer values.
Use the best guess
-
Long winded, but as a demo...
static void SumNumbersinDate()
{
//http://www.codeproject.com/Messages/4513781/I-have-string-1999220-how-to-parse.aspx
string theDate = "19881205";
char[] dateLetters = theDate.ToCharArray();
int[] dateNumbers = new int[theDate.Length];
int sum = 0;
for (int ind = 0; ind < theDate.Length; ind++)
{
Console.WriteLine("Current Total: " + sum.ToString());
dateNumbers[ind] = int.Parse(dateLetters[ind].ToString());
Console.WriteLine("Adding: " + dateNumbers[ind].ToString());
sum += dateNumbers[ind];
Console.WriteLine("New Total: " + sum.ToString());
}
Console.WriteLine("The sum of the numbers is: " + sum.ToString());
Console.WriteLine("Press any key to close the demo...");
Console.ReadKey(true);
} -
I have develop simple application first I take birthday from user and calculate his lucky number from sum of the values of the birthday Ex: '19881205' How i get the sum of values in this string?
Integer.TryParse
is often the best solution since it ensures you are parsing an integer and if not, does not throw an error.WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial
-
I have develop simple application first I take birthday from user and calculate his lucky number from sum of the values of the birthday Ex: '19881205' How i get the sum of values in this string?
-
Do you also have to verify that a date is valid? I'd hazard a guess that maybe you should. Garbage-In = Garbage-Out
Q. Hey man! have you sorted out the finite soup machine? A. Why yes, it's celery or tomato.
Since OP needs this to calculate a "lucky number" I wouldn't worry too much about "garbage". ;)
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian