Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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!
Posted

1 solution

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:
C#
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);
 
Share this answer
 
v3
Comments
riodejenris14 5-Jun-13 5:39am    
ya but what you mean by the second part i dont understand?
StM0n 5-Jun-13 5:40am    
"i want to dispay the number as XXXXXX and mail id as XXXXX" -> second part :)

If you use the regex, then you could obtain this data.
riodejenris14 5-Jun-13 5:48am    
s i tried with regex but i couldnt acheive it can u explain me with sample code or condition which helps me out better.
StM0n 5-Jun-13 6:45am    
Here you go...
riodejenris14 5-Jun-13 7:34am    
thnx man!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900