从名称末尾获取号码 [英] Get number from end of name

查看:56
本文介绍了从名称末尾获取号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建函数


static int ItemNumber(string itemName)


从名称末尾返回数字


ItemNumber(" Item1")返回1

ItemNumber(" Other1")返回1

ItemNumber(" Item123")返回123



怎么创建这个?


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.

推荐答案

8月7日,12:09 * pm,Andrus < kobrule ... @ hot.eewrote:
On Aug 7, 12:09*pm, "Andrus" <kobrule...@hot.eewrote:

我需要创建函数*

static int ItemNumber(string itemName)


从名称末尾返回数字


ItemNumber(" Item1")*返回* 1

ItemNumber(" Other1")*返回* 1

ItemNumber(" Item123")*返回* 123




如何创建这个?


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.



使用.Substring方法循环遍历字符串,同时指定一个

字符。尝试将角色转换为

Try / Catch块中的数字。当功能成功时,那么从那个索引

位置就是数字,你可以用

另一个.Substring调用来接它。

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.


8月7日下午5:16,za ... @ construction-imaging.com写道:
On Aug 7, 5:16 pm, za...@construction-imaging.com wrote:

循环通过字符串使用.Substring方法指定一个

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



无缘无故地创建大量字符串是没有意义的。使用

索引器返回一个char:


char c = text [i];

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

char c = text[i];


尝试将角色转换为

Try / Catch块中的数字。
Try to convert the character to a number in a
Try/Catch block.



Ick no。使用Character.IsDigit - 然后你没有使用

流控制的例外。

Ick no. Use Character.IsDigit - then you''re not using exceptions for
flow control.


当函数成功时,那么index

的位置是数字,你可以用

另一个.Substring调用来取消它。
When the function is successful, then from that index
position on is the number, and you can pick it off with
another .Substring call.



否,这将失败foo5bar10。我们需要从最后开始工作。

我建议类似:


public static int ParseNumberAtEnd(string text)

{

for(int i = text.Length-1; i> = 0; i--)

{

if( !Character.IsDigit(text [i]))

{

if(i == text.Length - 1)

{

抛出新的ArgumentException(末尾没有数字);

}

string digits = text.Substring(i + 1);

返回int.Parse(数字);

}

}

//文本完全由数字组成!

返回int.Parse(文本);

}


或者,Andrus可以使用类似@" \\的正则表达式\\ n

+ ^" (可能是

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[i]))
{
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


而不是^ - 我永远不会记得他们走的路是哪一回路。)


Jon
instead of ^ - I can never remember which way round
they go.)

Jon


这篇关于从名称末尾获取号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆