views:

119

answers:

8

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

+3  A: 

You can use the Regex.Replace method:

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

Kris Krause
Upvote: same answer as mine, but better documented (and faster-to-reply :)
mikemanne
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
@kbrimington see my solution for such a regex: http://stackoverflow.com/questions/3704345/replace-char-in-a-string/3704464#3704464
Ahmad Mageed
+1  A: 

you can chain replace

var newstring =    "[email protected]".Replace("@","_").Replace(".","_");
Pharabus
This doesn't work for [email protected]. "i still want the output to look like XX.X_YYY_ZZZ" says the OP.
Zafer
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?
Pharabus
A: 

Regular Expressions can be very complex to learn and decipher, but are also very powerful. I'm confident a regex could be defined which would support this sort of specialized replacement behavior.

mikemanne
+3  A: 

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();
}
kbrimington
Zafer
this works perfect.. thank you
A: 

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

snkmchnb
+8  A: 

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('.', '_');
Bob
+1, solves the problem, and does so simply and quickly.
Bloodyaugust
correct for a faster method.. thanks
+3  A: 

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.

Ahmad Mageed
+1: Nicely done. I've not seen `(??)` syntax before. Can you recommend a resource where I can read up on it?
kbrimington
@kbrimington thanks! You can find some nice examples of positive/negative look-arounds in [The 30 Minute Regex Tutorial](http://www.codeproject.com/KB/dotnet/regextutorial.aspx) (see examples 22-30). Also see Regular-Expressions.info's look-around pages: [Page1](http://www.regular-expressions.info/lookaround.html) and [Page2](http://www.regular-expressions.info/lookaround2.html).
Ahmad Mageed
A: 

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(".", "_");
Zafer