将 at 之后的字符串替换为另一个字符串

Replace string after at with another string

我有两个字符串。

第一个字符串:

"31882757623";epid=5440626C04;tag=daa784a738

第二个字符串:

vandrielfinance.nl

我想用第二个字符串 (vandrielfinance.nl) 将 @ 之后的第一个字符串中的 asklync.nl 替换为 vandrielfinance.nl。其他一切都将保持不变。

因此新字符串将是:

"31882757623";epid=5440626C04;tag=daa784a738

这是我目前的情况:

static string ReplaceSuffix(string orginal, string newString)
{
    string TobeObserved = "@";
    orginal = "\"31882757623\"<sip:+31882757623@asklync.nl;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;
}

我不是 string.Split() 的忠实粉丝,但在这种情况下它有助于快速完成工作:

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;
}

如果原始域真的总是 asklync.nl,您甚至可以这样做:

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;
} 

这应该有效

        var orginal = "\"31882757623\"<sip:+31882757623@asklync.nl;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;

为什么不 string.Replace?

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

我不是 RegEx 或 string.Split 的超级粉丝,尤其是当字符串函数已经存在以替换字符串的一部分时。

    string orginal  = "\"31882757623\"<sip:+31882757623@asklync.nl;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);