0

how to change

[email protected] into XXX_YYY_ZZZ

One way i know is to use the string.replace(char, char) method,

but i want to replace "@" & "." The above method replaces just one char.

one more case is what if i have [email protected]...

i still want the output to look like XX.X_YYY_ZZZ

Is this possible?? any suggestions thanks

7 Answers 7

8

So, if I'm understanding correctly, you want to replace @ with _, and . with _, but only if . comes after @? If there is a guaranteed @ (assuming you're dealing with e-mail addresses?):

string e = "[email protected]";
e = e.Substring(0, e.IndexOf('@')) + "_" + e.Substring(e.IndexOf('@')+1).Replace('.', '_');
Sign up to request clarification or add additional context in comments.

Comments

4

Here's a complete regex solution that covers both your cases. The key to your second case is to match dots after the @ symbol by using a positive look-behind.

string[] inputs = { "[email protected]", "[email protected]" };
string pattern = @"@|(?<=@.*?)\.";

foreach (var input in inputs)
{
    string result = Regex.Replace(input, pattern, "_");
    Console.WriteLine("Original: " + input);
    Console.WriteLine("Modified: " + result);
    Console.WriteLine();
}

Although this is simple enough to accomplish with a couple of string Replace calls. Efficiency is something you will need to test depending on text size and number of replacements the code will make.

2 Comments

+1: Nicely done. I've not seen (??) syntax before. Can you recommend a resource where I can read up on it?
@kbrimington thanks! You can find some nice examples of positive/negative look-arounds in The 30 Minute Regex Tutorial (see examples 22-30). Also see Regular-Expressions.info's look-around pages: Page1 and Page2.
3

You can use the Regex.Replace method:

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.replace(v=VS.90).aspx

3 Comments

Upvote: same answer as mine, but better documented (and faster-to-reply :)
I'd be interested in seeing the regex that meets the OP's needs (i.e., replaces @ and . except when the . is before the @.)
@kbrimington see my solution for such a regex: stackoverflow.com/questions/3704345/replace-char-in-a-string/…
3

You can use the following extension method to do your replacement without creating too many temporary strings (as occurs with Substring and Replace) or incurring regex overhead. It skips to the @ symbol, and then iterates through the remaining characters to perform the replacement.

public static string CustomReplace(this string s)
{
    var sb = new StringBuilder(s);
    for (int i = Math.Max(0, s.IndexOf('@')); i < sb.Length; i++)
        if (sb[i] == '@' || sb[i] == '.')
            sb[i] = '_';
    return sb.ToString();
}

Comments

0

you can chain replace

var newstring =    "[email protected]".Replace("@","_").Replace(".","_");

3 Comments

This doesn't work for [email protected]. "i still want the output to look like XX.X_YYY_ZZZ" says the OP.
this works but my problem is for the above input i get XX_X_YYY_ZZZ whereas what i wanted was XX.X_YYY_ZZZ
@user175084 ah, sorry didnt notice, you need the first period to remain?
0

Create an array with characters you want to have replaced, loop through array and do the replace based off the index.

Comments

0

Assuming data format is like [email protected], here is another alternative with String.Split(char seperator):

string[] tmp = "[email protected]".Split('@');
string newstr = tmp[0] + "_" + tmp[1].Replace(".", "_");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.