Get number from end of name

  • Thread starter Thread starter Andrus
  • Start date Start date
A

Andrus

I need to create function

static int ItemNumber(string itemName)

which returns number from end of name

ItemNumber("Item1") returns 1
ItemNumber("Other1") returns 1
ItemNumber("Item123") returns 123


etc.
How to create this ?

Andrus.
 
I need to create function  

static int ItemNumber(string itemName)

which returns number from end of name

ItemNumber("Item1")  returns  1
ItemNumber("Other1")  returns  1
ItemNumber("Item123")  returns  123

etc.
How to create this ?

Andrus.

Loop through the string with the .Substring method specifying one
character at at time. Try to convert the character to a number in a
Try/Catch block. When the function is successful, then from that index
position on is the number, and you can pick it off with
another .Substring call.
 
Loop through the string with the .Substring method specifying one
character at at time.

There's no point in creating loads of strings for no reason. Using the
indexer returns a char:

char c = text;
Try to convert the character to a number in a
Try/Catch block.

Ick no. Use Character.IsDigit - then you're not using exceptions for
flow control.
When the function is successful, then from that index
position on is the number, and you can pick it off with
another .Substring call.

No, that would fail on "foo5bar10". We need to work back from the end.
I would suggest something like:

public static int ParseNumberAtEnd(string text)
{
for (int i = text.Length-1; i >= 0; i--)
{
if (!Character.IsDigit(text))
{
if (i == text.Length - 1)
{
throw new ArgumentException("No digits at end");
}
string digits = text.Substring(i+1);
return int.Parse(digits);
}
}
// Text is entirely made of digits!
return int.Parse(text);
}

Alternatively, Andrus could use a regex of something like @"\d
+^" (Possibly $ instead of ^ - I can never remember which way round
they go.)

Jon
 
Alternatively, Andrus could use a regex of something like @"\d
+^" (Possibly $ instead of ^ - I can never remember which way round
they go.)

$ in this case, to anchor the end.
^ anchors the beginning.


LastIndexOfAny might also be a reasonable solution, though I would expect
Char.IsDigit to be faster.
 
Along the same lines as Jon's reply, here is a fun way if you are
using Framework 3.5:

int.Parse("0" +
item.Substring(Array.LastIndexOf<char>(item.ToCharArray(),
item.ToCharArray().LastOrDefault<char>(c => !Char.IsDigit(c))) + 1))

Long line, but nice trick. Just an FYI.
jake


Loop through the string with the .Substring method specifying one
character at at time.

There's no point in creating loads of strings for no reason. Using the
indexer returns a char:

char c = text;
Try to convert the character to a number in a
Try/Catch block.

Ick no. Use Character.IsDigit - then you're not using exceptions for
flow control.
When the function is successful, then from that index
position on is the number, and you can pick it off with
another .Substring call.

No, that would fail on "foo5bar10". We need to work back from the end.
I would suggest something like:

public static int ParseNumberAtEnd(string text)
{
for (int i = text.Length-1; i >= 0; i--)
{
if (!Character.IsDigit(text))
{
if (i == text.Length - 1)
{
throw new ArgumentException("No digits at end");
}
string digits = text.Substring(i+1);
return int.Parse(digits);
}
}
// Text is entirely made of digits!
return int.Parse(text);

}

Alternatively, Andrus could use a regex of something like @"\d
+^" (Possibly $ instead of ^ - I can never remember which way round
they go.)

Jon
 
How many other ways can we skin this cat? :)

Create a parameterised SQL query and get the query engine to do the
work? Or we could always set up a web service for it, and maybe write a
custom LINQ provider to call that web service.
 
Paul E Collins said:
But are digits recognised by Char.IsDigit guaranteed to parse as integers?
Even on a Western system, Hebrew numerals would count as digits (in the
character sense), wouldn't they?

Hmm... not sure. It's only *decimal* digits, but I don't know what
other characters are in DecimalDigitNumber.
 
Paul E Collins said:
char ch = (char) 0x667; // Arabic-Indic Digit Seven
Console.WriteLine(Char.IsDigit(ch)); // "True"
int i = Int32.Parse(ch.ToString()); // throws FormatException

I suppose Int32.Parse *might* work differently depending on your system
locale, but in either case Char.IsDigit will check whether the character is
a digit in *any* locale, so it's not safe to use for this purpose.

Yup. It's a shame there isn't a property for this, as it's reasonably
common.
 
Jon said:
Yup. It's a shame there isn't a property for this, as it's reasonably
common.

Cue requests for extension properties.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top