Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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?
In my requirement it is fetching ((startTime==134)&&(endTime==1254)) from xml file where the value of startTime and endTimekeeps on changing based on manipulation from below code

Here is the code:

C#
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());
Posted
Updated 1-Mar-15 19:00pm
v3
Comments
[no name] 2-Mar-15 0:19am    
Here ((startTime==134)&&(endTime==1254)),
only the integer values are going to change, rest all remains the same ?
Member 11380284 2-Mar-15 0:53am    
Yes only integer values are going to change

Hi,

Pleae try the following code:
C#
string joinedstring;
string split = "&&";
string time1 = "154";
string time2 = "1234";
string s = "((startTime==134)&&(endTime==1254))";
string[] lines = Regex.Split(s, split);
Console.WriteLine(s);
Console.WriteLine(Regex.Replace(lines[0], @"\d+", time1) + split + Regex.Replace(lines[1], @"\d+", time2));
Console.ReadKey();


I tried that using RegEx. It worked for me.

Hope this helps !! :) :)

Revert in case anything else is required.
 
Share this answer
 
v4
Comments
[no name] 2-Mar-15 0:56am    
Regex.Replace(lines[0], @"\d+", time1) + split + Regex.Replace(lines[0], @"\d+", time2)

This will do exactly what you want as an output.
If I understood your question correctly, just use String.Replace Method (String, String)[^]
 
Share this answer
 
Hi,

some time i faced some problem for replace in a string.

lets assume, i have string "1234gg1234567" and i want to replace "1234" from starting position only OR only from end.

how will i do?

try this code. modify according to you need. This is based on index of a character in a string.

C#
string str = "((startTime==134)&&(endTime==1254))";

int pos_start_1 = str.IndexOf("==") + 2;
int pos_end_1 = str.IndexOf(")&&(");
int pos_start_2 = str.LastIndexOf("==") + 2;
int pos_end_2 = str.IndexOf("))");

string time1 = str.Substring(pos_start_1, (pos_end_1 - pos_start_1));
string time2 = str.Substring(pos_start_2, (pos_end_2 - pos_start_2));

string time1_new = "time1_new";
string time2_new = "time1_new";

string newString = str.Replace(time1, time1_new).Replace(time2, time2_new);
 
Share this answer
 
v2
Comments
Santosh K. Tripathi 2-Mar-15 1:30am    
i could not understand, why it is down voted?
[no name] 2-Mar-15 23:00pm    
There is always a debate that, which is faster, indexOf or the regex.
See the example here: http://ayende.com/blog/2930/regex-vs-string-indexof

In my opinion that depends upon the scenario. Here, the maintenance of the code is easier is case of regex. Also, for a new developer who is seeing the code, it is easier for him to understand the code if we apply the regex.

I also think that using the hard coded indexes can make it difficult for the programmers.

But great job with the solution. It indeed is a solution.

Cheers!!
Santosh K. Tripathi 2-Mar-15 23:09pm    
thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900