R
raylopez99
Refer to Int32.TryParse method: http://msdn.microsoft.com/en-us/library/zf50za27.aspx
if you have to, specifically this example:
numericString = "10345.72"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
Which failed to convert the string "10345.72" to the int 10345,
because of the extra digits after the "."
My question is this: is there a neat "LINQ" type way of parsing such
a string so that the first time a "." (period) is found, the string is
extracted, and then I can use Int32.TryParse?
I know I can solve this problem using a character buffer, reading each
Unicode byte into the buffer, then stopping the first time I find a
".", then reading out the buffer to create a string, then using
Int32.TryParse to extract the int from the string. That is, perhaps I
can use the StringBuilder method to CopyTo a char array, then use a
trick to extract the numeric string before the "."
For example--
StringBuilder sb = new StringBuilder ("10345.72");
int count = sb.Length;
char[] temporary_charBuf1 = new char[100]; // 1 of 2
sb.Copy(0,temporary_charBuf1,0,count);
char[] charBuf2 = new char [100] //2 of 2
foreach (char c in temporary_charBuf) {//code here: if char is not a
period, fill charBuf2, otherwise not and break once a period is found}
string numericString = "";
numericString = new string(charBuf2); // Create new string passing
charBuf2 into the constructor
Now your numericString should be 10345 from the original string
"10345.72".
But I'm looking for a faster or shorter way (since this is not a big
deal, and I don't want to spend too many lines of code making it
work). As you can see from the above, which does not even include the
code inside the foreach loop, it's a lot of code.
Can we replace this with one line of LINQ (plus I'm trying to learn
LINQ as I go)?
No big deal (pseudo code is OK)...just curious.
RL
if you have to, specifically this example:
numericString = "10345.72"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
Which failed to convert the string "10345.72" to the int 10345,
because of the extra digits after the "."
My question is this: is there a neat "LINQ" type way of parsing such
a string so that the first time a "." (period) is found, the string is
extracted, and then I can use Int32.TryParse?
I know I can solve this problem using a character buffer, reading each
Unicode byte into the buffer, then stopping the first time I find a
".", then reading out the buffer to create a string, then using
Int32.TryParse to extract the int from the string. That is, perhaps I
can use the StringBuilder method to CopyTo a char array, then use a
trick to extract the numeric string before the "."
For example--
StringBuilder sb = new StringBuilder ("10345.72");
int count = sb.Length;
char[] temporary_charBuf1 = new char[100]; // 1 of 2
sb.Copy(0,temporary_charBuf1,0,count);
char[] charBuf2 = new char [100] //2 of 2
foreach (char c in temporary_charBuf) {//code here: if char is not a
period, fill charBuf2, otherwise not and break once a period is found}
string numericString = "";
numericString = new string(charBuf2); // Create new string passing
charBuf2 into the constructor
Now your numericString should be 10345 from the original string
"10345.72".
But I'm looking for a faster or shorter way (since this is not a big
deal, and I don't want to spend too many lines of code making it
work). As you can see from the above, which does not even include the
code inside the foreach loop, it's a lot of code.
Can we replace this with one line of LINQ (plus I'm trying to learn
LINQ as I go)?
No big deal (pseudo code is OK)...just curious.
RL