Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace substring in a string in c#

Tags:

c#

I want to replace the substrings 134 and 1254 in a string

((startTime==134)&&(endTime==1254))

with some dynamic value - say, for example, 154 and 1234 respectively. I have written the code to place using String.Split method but it seems the code is very long. 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());
like image 848
amol saxena Avatar asked Mar 02 '15 03:03

amol saxena


1 Answers

Replace chars in a string using replace method as shown here

string output = input.Replace("old_value", "new_value");
like image 104
Fahim Avatar answered Nov 01 '22 18:11

Fahim