Niels hoi
Niels hoi

Reputation: 71

Replace string after at with another string

I have two strings.

First string:

"31882757623"<sip:[email protected];user=phone>;epid=5440626C04;tag=daa784a738

Second string:

vandrielfinance.nl

I want to replace asklync.nl to vandrielfinance.nl in the first string after the @ with the second string (vandrielfinance.nl). Everything else will stay the same.

So the new string will be:

"31882757623"<sip:[email protected];user=phone>;epid=5440626C04;tag=daa784a738

Here is what I have so far:

static string ReplaceSuffix(string orginal, string newString)
{
    string TobeObserved = "@";
    orginal = "\"31882757623\"<sip:[email protected];user=phone>;epid=5440626C04;tag=daa784a738";
    string second = "vandrielfinance.nl";
    string pattern = second.Substring(0, second.LastIndexOf("@") + 1);
    string code = orginal.Substring(orginal.IndexOf(TobeObserved) + TobeObserved.Length);

    //newString = Regex.Replace(code,second, pattern);
    newString = Regex.Replace(second, orginal, pattern);
    string hallo = orginal.Replace(newString, second);

    Console.Write("Original String: {0}", orginal);
    Console.Write("\nReplacement String: \n{0}", newString);

    Console.WriteLine("\n" + code);

    return newString;
}

Upvotes: 1

Views: 93

Answers (4)

AWinkle
AWinkle

Reputation: 673

I'm not a huge fan of RegEx or string.Split, especially when a string function already exists to replace a portion of a string.

    string orginal  = "\"31882757623\"<sip:[email protected];user=phone>;epid=5440626C04;tag=daa784a738";
    string second  = "vandrielfinance.nl";
    int start = orginal .IndexOf("@");
    int end = orginal .IndexOf(";", start);
    string newString = orginal .Replace(orginal.Substring(start, end-start), second );
    Console.WriteLine(orginal );
    Console.WriteLine(newString);

Upvotes: 0

DrewJordan
DrewJordan

Reputation: 5314

why not string.Replace?

string s = "\"31882757623\"<sip:[email protected];user=phone>;epid=5440626C04;tag=daa784a738";
            string t = "vandrielfinance.nl";
            string u = s.Replace("asklync.nl", t);
            Console.WriteLine(u);

Upvotes: 2

Joel Coehoorn
Joel Coehoorn

Reputation: 416039

I'm not really a fan a string.Split(), but it made for quick work in this case:

static string ReplaceSuffix(string orginal, string newString)
{
    var segments = original.Split(";".ToCharArray());
    var segments2 = segments[0].Split("@".ToCharArray());
    segments2[1] = newString;
    segments[0] = string.Join("@", segments2);
    var result = string.Join(";", segments);

    Console.WriteLine("Original String:\n{0}\nReplacement String:\n{1}, original, result);

    return result;
}

If the original domain will really always be asklync.nl, you may even be able to just do this:

static string ReplaceSuffix(string orginal)
{
    var oldDomain = "asklync.nl";
    var newDomain = "vandrielfinance.nl";
    var result = original.Replace(oldDomain, newDomain);

    Console.WriteLine("Original String:\n{0}\nReplacement String:\n{1}, original, result);

    return result;
} 

Upvotes: 0

Lareau
Lareau

Reputation: 2011

This should work

        var orginal = "\"31882757623\"<sip:[email protected];user=phone>;epid=5440626C04;tag=daa784a738";
        string second = "vandrielfinance.nl";
        var returnValue = string.Empty;

        var split = orginal.Split('@');
        if (split.Length > 0)
        {
            var findFirstSemi = split[1].IndexOf(";");
            var restOfString = split[1].Substring(findFirstSemi, split[1].Length - findFirstSemi);

            returnValue = split[0] + "@" + second + restOfString;
        }

        Console.WriteLine("Original String:");
        Console.WriteLine("{0}", orginal);
        Console.WriteLine("Replacement String:");
        Console.WriteLine("{0}", returnValue);

        //return returnValue;

Upvotes: 0

Related Questions