Search and replace string method

I need to find a specific part of the string value, as shown below, I need to change the "Meeting ID" to a specific number.

This number comes from a drop-down list of several numbers, so I can’t just use search and replace. Because the text can change to one of several numbers before the user is happy.

"0783", the portion of the line never changes, and the "meeting ID" always follows ",".

So I need to go to "0783, INSERT TEXT " and then insert the new number in 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 to find this line and replace the test every time?

Hope these guys?

+5
source share
4 answers

Okay, so there are several ways to do this, however it seems to be a line you have control over, so I'm going to say here 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 this, you should be a little smarter:

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);
+3
source

You must save your β€œcurrent” Meeting IDin a variable by changing it along with your user actions, and then use the same global variable whenever you need a string.

, , . /, - .

+1

You can try using the method Regex.Replace

    string pattern = @"\d{3},";
    Regex regex = new Regex(pattern);

    var inputStr = "518-06-xxx, 9999 999 0783";
    var replace = "..."
    var outputStr = regex.Replace(inputStr, replace);
0
source

use Regex.Split with the "0783" token, then on the second line in the return split by token "," array, the first element in the string array will be where you insert the new text. Then use string.Join to join the first split with "0783", and attach the second to ",".

        string temp = "Business Invitation, start time, M Problem, 518-06-xxx, 9999 999 0783, Meeting ID, xxx ??";
        string newID = "1234";
        string[] firstSplits = Regex.Split(temp, "0783,");
        string[] secondSplits = Regex.Split(firstSplits[1], ",");
        secondSplits[0] = newID;
        string @join = string.Join(",", secondSplits);
        firstSplits[1] = @join;

        string newString = string.Join("0783,", firstSplits);
0
source

All Articles