用at替换另一个字符串后的字符串

时间:2015-02-12 19:56:15

标签: c# c#-5.0

我有两个字符串。

第一个字符串:

"31882757623"<sip:+31882757623@asklync.nl;user=phone>;epid=5440626C04;tag=daa784a738

第二个字符串:

vandrielfinance.nl

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

所以新字符串将是:

"31882757623"<sip:+31882757623@vandrielfinance.nl;user=phone>;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;
}

4 个答案:

答案 0 :(得分:2)

为什么不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);

答案 1 :(得分:0)

我不是一个真正的粉丝.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;
} 

答案 2 :(得分:0)

这应该有效

        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;

答案 3 :(得分:0)

我不是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);