5

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?

4 Answers 4

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

This looks like the right idea, I'll try this when i get chance! Thanks.
No problem @Derek, I'm glad I could be of assistance!
1

You should store your "current" Meeting ID in a variable, changing it along with your user's actions, and then use that same global variable whenever you need the string.

This way, you don't have to worry about what's inside the string and don't need to mess with array indexes. You will also be safe from magic numbers / strings, which are bound to blow up in your face at some point in the future.

Comments

0

You can try with Regex.Replace method

    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);

Comments

0

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

        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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.