大家好, 我有这个: 这个字符串: 31882757623< sip:+31882757623@asklync.nl; user = phone> ;; epid = 5440626C04; tag = daa784a738,vandrielfinance.nl 必须由此替换:
Hi everybody, I have this: this string: ""31882757623"<sip:+31882757623@asklync.nl;user=phone>;epid=5440626C04;tag=daa784a738", "vandrielfinance.nl" has to be replaced by this:
""31882757623"<sip:+31882757623@vandrielfinance.nl;user>;epid=5440626C04;tag=daa784a738"<br />我有:
I have this:
<pre lang="xml">static string ReplaceSuffix(string orginal, string newString) { newString = orginal.Substring(0, orginal.LastIndexOf(",")); newString.Replace(orginal, newString); Console.WriteLine(newString); return newString; }但是我不知道我必须填写主要方法:
but what I dont know what I have to fill in for the main method:
static void Main(string[] args) { //---****Second function---*** string OriginalSecond = "\"31882757623\"<sip:+31882757623@asklync.nl;user=phone>;epid=5440626C04;tag=daa784a738,\" \"vandrielfinance.nl\""; string newString; ReplaceSuffix(OriginalSecond, newString ); Console.ReadKey(); }谢谢 哦,我没有喜欢这样:
Thank you Oke, I have it now like this:
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 = orginal.Substring(0, orginal.LastIndexOf("@")); newString = Regex.Replace(orginal,second, pattern); //newString = orginal.Substring(0, orginal.LastIndexOf(",")); //newString.Replace(orginal, newString); Console.Write("Original String: {0}", orginal); Console.Write("\nReplacement String: \n{0}", newString); //Console.WriteLine(newString); return newString; }但如何更换:asklync.nl 与此:vandrielfinance.nl; 所以新字符串将是这样的: \31882757623 \< sip:+31882757623@vandrielfinance.nl; user> ;; epid = 5440626C04; tag = daa784a738,\\vandrielfinance.nl \; 谢谢
But how to replace this: asklync.nl with this:"vandrielfinance.nl"; so the new string will be like this: "\"31882757623\"<sip:+31882757623@vandrielfinance.nl;user>;epid=5440626C04;tag=daa784a738,\" \"vandrielfinance.nl\""; Thank you
推荐答案你没有替换任何东西。字符串是不可变的。特别是,所有字符串函数都返回一个字符串对象的全新实例 - 替换的结果,子字符串的提取,任何东西。你忽略了这个结果,而不是将它分配给任何东西。所以,你返回 newString 因为它是在第一行中分配的,在替换之前。 同样,你设法忽略了结果您自己的 ReplaceSuffix 在哪里调用它。
谢尔盖是对的。字符串是不可变的。 初看起来,我有一个问题:为什么要强行打开门?您可以使用随时可用的 String.Replace方法(字符串,字符串) ) [ ^ ] 另一方面,你可以拆分 [ ^ ]将字符串分成几部分,然后加入 [ ^ ]创建新的。 如需了解更多信息,请参阅:如何to:Split Strings(C#编程指南) [ ^ ] Sergey is right. String is immutable. At the first look, i have one question: Why to force doors wide open? You can use ready-to-use String.Replace Method (String, String)[^] On the other hand you can Split[^] string into parts and then Join[^] to create new one. For further information, please see: How to: Split Strings (C# Programming Guide)[^]
static string ReplaceSuffix(string orginal, string newString) { var sentence = orginal.Split(";".ToCharArray()); var sentence2 = sentence[0].Split("@".ToCharArray()); sentence2[1] = newString; sentence[0] = string.Join("@", sentence2); var result = string.Join(";", sentence); Console.WriteLine("Original String:\n{0}\nReplacement String:\n{1}", orginal, result); return result; }