I have a email id like below
string email=test.mail@test.com;
string myText = email.split(".");
i am not sure who split first two characters and followed by two characters after the period or dot.
myText = tema //(desired output)
I have a email id like below
string email=test.mail@test.com;
string myText = email.split(".");
i am not sure who split first two characters and followed by two characters after the period or dot.
myText = tema //(desired output)
Use LINQ ;)
string myText = string.Join("", email.Remove(email.IndexOf('@')).Split('.')
.Select(r =>new String(r.Take(2).ToArray())));
@
, (including @
).
String
constructor creating a stringString.Join
to combine returned strings element. Another Linq solution:
string first = new string(email.Take(2).ToArray());
string second = new string(email.SkipWhile(c => c != '.').Skip(1).Take(2).ToArray());
string res = first + second;
string.Join(string.Empty, email.Substring(0, email.IndexOf("@")).Split('.').Select(x => x.Substring(0, 2)));
Lots of creative answers here, but the most important point is that Split()
is the wrong tool for this job. It's much easier to use Replace()
:
myText = Regex.Replace(email, @"^(\w{2})[^.]*\.(\w{2})[^.]*@.+$", "$1$2");
Note that I'm making a lot of simplifying assumptions here. Most importantly, I'm assuming the original string contains the email address and nothing else (you're not searching for it), that the string is well formed (you're not trying to validate it), and that both of substrings you're interested in start with at least two word characters.