Get nummerical part from string

  • Thread starter Thread starter spmm#
  • Start date Start date
S

spmm#

Hi!

I want to get the nummerical part from a string and put that in an integer.
For example if I enter "ns00086"; I want to get 86 back. Is there an easy
way of doing this?

So far I've come up with this method, but I'm suspecting there is a more
efficient way of doing it...

private int GetNummericalPartFromString(string myString)
{
char[] charArray;
string s = "0";
charArray = myString.ToCharArray();
foreach(char c in charArray)
{
if(char.IsDigit(c))
{
s = s + c;
}
}
return int.Parse(s);
}
 
spmm# said:
Hi!

I want to get the nummerical part from a string and put that in an integer.
For example if I enter "ns00086"; I want to get 86 back.

How about if you enter "x5xxx5"? Would you want 55 back or 5, for example?
Or is this something that won't happen?
Is there an easy way of doing this?

Probably, but not within the framework, as far as I know.
So far I've come up with this method, but I'm suspecting there is a more
efficient way of doing it...

private int GetNummericalPartFromString(string myString)
{
char[] charArray;
string s = "0";
charArray = myString.ToCharArray();
foreach(char c in charArray)
{
if(char.IsDigit(c))
{
s = s + c;
}
}
return int.Parse(s);
}

First, there's no need to create the char array. Second, appending a
string in that way is inefficient. Third, the method should be static
since it doesn't operate on instance state. Fourth, 'numerical' is
misspelt. :-)

private static int GetNumericalPartFromString(string myString)
{
StringBuilder sb = new StringBuilder("0");
foreach (char c in myString)
{
if (Char.IsDigit(c))
{
sb.Append(c);
}
}
return Int32.Parse(sb.ToString());
}

Note: no checking that sb.ToString() can be parsed without error is
performed here.
 
How about if you enter "x5xxx5"? Would you want 55 back or 5, for
example?
Or is this something that won't happen?
Yes, I'd want 55 back.

Why is a StringBuilder more efficient than what I did?? Thanks for you
solution!
 
How about if you enter "x5xxx5"? Would you want 55 back or 5, for
example?
Or is this something that won't happen?
Yes, I'd want 55 back.

Why is a StringBuilder more efficient than what I did?? Thanks for you
solution!
 
spmm# said:
Yes, I'd want 55 back.

Why is a StringBuilder more efficient than what I did?? Thanks for you
solution!

Because StringBuilder doesn't end up creating an extra string on every
iteration. See a thread called "reading in text" for more information
abuot this.
 
spmm# said:
Why is a StringBuilder more efficient than what I did?

Because when you say:

myString = myString + "foo";

a **whole new** string object is made. This is because strings are
immutable -- a specific instance cannot be changed; to effect changes, one
must create a whole new instance based on the original instance.

StringBuilder is mutable and thus doesn't have this "problem".
 
Why do you need to save it in a string (or strinbuilder) at all,

private int GetNumericalPartFromString(string myString){
char[] charArray= myString.ToCharArray();
string i = 0;
foreach(char c in charArray)
{
if(char.IsDigit(c))
{
i = (i * 10) + int.Parse(c);
}
}
return i;
}
 
C# Learner said:
spmm# said:
Hi!

I want to get the nummerical part from a string and put that in an
integer.
For example if I enter "ns00086"; I want to get 86 back.

How about if you enter "x5xxx5"? Would you want 55 back or 5, for
example?
Or is this something that won't happen?
Is there an easy way of doing this?

Probably, but not within the framework, as far as I know.
So far I've come up with this method, but I'm suspecting there is a more
efficient way of doing it...

private int GetNummericalPartFromString(string myString)
{
char[] charArray;
string s = "0";
charArray = myString.ToCharArray();
foreach(char c in charArray)
{
if(char.IsDigit(c))
{
s = s + c;
}
}
return int.Parse(s);
}

First, there's no need to create the char array. Second, appending a
string in that way is inefficient. Third, the method should be static
since it doesn't operate on instance state. Fourth, 'numerical' is
misspelt. :-)

private static int GetNumericalPartFromString(string myString)
{
StringBuilder sb = new StringBuilder("0");
foreach (char c in myString)
{
if (Char.IsDigit(c))
{
sb.Append(c);
}
}
return Int32.Parse(sb.ToString());
}

Note: no checking that sb.ToString() can be parsed without error is
performed here.

Just for fun, why use the int.Parse at all???

private static int GetNumericalPartFromString(string myString)
{
int result = 0;

foreach (char c in myString)
{
if (Char.IsDigit(c))
result = checked (result * 10 + (c - '0'));
}

return result;
}
 
Well,

In your solution I would need a string, because int.Parse expects a string.
Stefan Simek's solution does work though. I assume this is more efficient
than using StringBuilder, is that right???

Thanks!
 
spmm# said:
In your solution I would need a string, because int.Parse expects a string.
Stefan Simek's solution does work though. I assume this is more efficient
than using StringBuilder, is that right???

The algorithm used in Stefan's code is more efficient.
 
Back
Top