Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Find & Replace Method

Tags:

string

c#

I need to locate a specific part of a string value like the one below, I need to alter the "Meeting ID" to a specific number.

This number comes from a dropdownlist of multiple numbers, so I cant simply use find & replace. As the text could change to one of multiple numbers before the user is happy.

The "0783," part of the string never changes, and "Meeting ID" is always followed by a ",".

So i need to get to "0783, INSERT TEXT ," and then insert the new number on the Index Changed event.

Here is an example :-

Business Invitation, start time, M Problem, 518-06-xxx, 9999 999 0783, Meeting ID, xxx ??

What is the best way of locating this string and replacing the test each time?

I hope this makes sense guys?

like image 468
Derek Avatar asked Oct 06 '22 12:10

Derek


1 Answers

Okay, so there are several ways of doing this, however this seems to be a string you have control over so I'm going to say here's what you want to do.

var myString = string.Format("Business Invitation, start time, M Problem, 518-06-xxx, 9999 999 0783, {0}, xxx ??", yourMeetingId);

If you don't have control over it then you're going to have to be a bit more clever:

var startingIndex = myString.IndexOf("0783, ");
var endingIndex = myString.IndexOf(",", startingIndex + 6);
var pattern = myString.Substring(startingIndex + 6, endingIndex - (startingIndex + 6));
myString = myString.Replace(pattern, yourMeetingId);
like image 132
Mike Perrenoud Avatar answered Oct 13 '22 12:10

Mike Perrenoud