如何检查C#中的字符串? [英] How to Check the String in C#?

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

问题描述

我的字符串是:



string str1 =嗨这是abc而我的手机号码是9876543210而我的邮件ID是abc@abc.com



解决方案需要:

如果字符串中的数字长度等于或超过10我希望将数字显示为XXXXXX且邮件ID为XXXXX



请用代码解释!

My string is:

string str1 = "hi this is abc and my mobile no is 9876543210 and my mail id is abc@abc.com"

Solution Need:
if the number length equals or exceeds 10 in the string i want to dispay the number as XXXXXX and mail id as XXXXX

pls explain with code!

推荐答案

对于第二部分,您可以使用像<$ c这样的正则表达式$ c>(\d {10,})。+ \((+ +?@。+ \ .. +)\ s 给你



1)所需数量

2)邮件ID



查看10+号码,你可以使用 \d {10,} 部分。



这样的东西:

For the second part you could use a regex like (\d{10,}).+\s(.+?@.+\..+)\s which gives you the

1) required number
2) the mail id

For checking the 10+ number, you could use the \d{10,} part.

Something like that:
var checkData = new Regex("\\d{10,}", 
      RegexOptions.CultureInvariant 
    | RegexOptions.Compiled);

    var extractData = new Regex("(\\d{10,}).+\\s(.+@.+\\..+)\\s", 
      RegexOptions.CultureInvariant 
    | RegexOptions.Compiled);

var test   = "hi this is abc and my mobile no is 9876543210 and my mail id is abc@abc.com sooooo";
var number = String.Empty;
var mail   = String.Empty;

if (checkData.IsMatch(test)) {

    var extractedData = extractData.Match(test);

    number = extractedData.Groups[1].Value;
    mail   = extractedData.Groups[2].Value;
    
} else {
    //Whatever happens here
}

Console.WriteLine(number);
Console.WriteLine(mail);


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

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