[英]How to replace substring in a string in c#
I want to replace the substrings 134 and 1254 in a string 我想用字符串替换子串134和1254
((startTime==134)&&(endTime==1254))
with some dynamic value - say, for example, 154 and 1234 respectively. 具有一些动态值 - 例如分别为154和1234。 I have written the code to place using
String.Split
method but it seems the code is very long. 我已经使用
String.Split
方法编写了代码,但似乎代码很长。 How can I make it shorter and more robust? 如何让它更短更健壮?
Here is the code: 这是代码:
string s = "((startTime==134)&&(endTime==1254))";
string[] time = s.Split(')').Reverse().ToArray();
var start = time.FirstOrDefault(s => s.Contains("startTime")).Split('=')[2];
var end = time.FirstOrDefault(e => e.Contains("endTime")).Split('=')[2];
start ="154";
end = "1234"
time[3] = "((startTime=="+start;
time[2] = "&&(endTime=="+end;
string joinedstring;
joinedstring= String.Join(")", time.Reverse());
使用替换方法替换字符串中的字符,如下所示
string output = input.Replace("old_value", "new_value");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.