检查字符串中的字母数字字符 [英] Checking of Alphanumeric characters in string

查看:88
本文介绍了检查字符串中的字母数字字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个字符串来自网关,响应为"AUTH/TKT T7564B"..
我应该将子字符串"T1564B"作为我的交易ID ...但是在某些情况下,该子字符串仅是数字字符,例如"AUTH/TKT 45286".
同样,我应该将该子字符串转换为Integer格式...我想知道如何检查该子字符串的字母数字字符并将其转换为int或引发异常...
请尽快帮助.....

谢谢
Radhika K

Hi,
I have one string which comes as response from gateway as ''AUTH/TKT T7564B''..
I should take sub string ''T1564B'' as my Transaction ID...But in some cases this sub string is just numeric characters only like ''AUTH/TKT 45286''.
Again I should convert that sub string to Integer format...I want to know How to check that sub string for Alphanumeric characters and convert it to int or else throw exception...
Please help soon.....

Thanks
Radhika K

推荐答案

使用Int32.TryParse

http://msdn.microsoft.com/en-us/library/f02979c7.aspx [ ^ ]
Use Int32.TryParse

http://msdn.microsoft.com/en-us/library/f02979c7.aspx[^]


尝试一下:
Try thisone:
public static Boolean isAlphaNumeric(string strToCheck)
{
    Regex rg = new Regex(@"^[a-zA-Z0-9\s,]*


"); 返回 rg.IsMatch(strToCheck); }
"); return rg.IsMatch(strToCheck); }


如果您在正则表达式中指定字符串应该包含什么,而不是它一定不能包含,那么这更难以理解.

在上面的示例中:

清单项目
^-表示字符串的开头
[] *-括号之间可以包含任意数量的字符
a-zA-Z0-9-任何字母数字字符
\ s-任何空格字符(空格/制表符等)
,-逗号


It''s more undestandable, if you specify in regex, what your string SHOULD contain, and not what it MUST NOT.

In the example above:

List item
^ - means start of the string
[]* - could contain any number of characters between brackets
a-zA-Z0-9 - any alphanumeric characters
\s - any space characters (space/tab/etc.)
, - commas


这篇关于检查字符串中的字母数字字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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