1

Asking for best practice for doing the following, having this string in C#:

string sentence = "I was watching TV last night while she was reading a book and you were playing video games";

I'm trying to get it automatically replaced by something like:

"you were watching TV last night while she was reading a book and I was playing video games"

Problem here is I can't use

sentence.Replace("I was", "you were").Replace("you were", "I was")

because final sentence would look bad: "I was watching tv while she was reading a book and I was playing video games".

It might look simple but it is not as simple as it seems, can I get some advice please?

6
  • One of hte overloads of Replace allows you to defined the first & last Index it should cover. | You could also expand the replace strings to include "watching" and "playing" respeectively. Expanding the Search string to filler stuff is a common way to avoid fake positives. | However overall this belongs into the area of Language Science and Artificial Intelligence, not normal programming. Commented Nov 19, 2019 at 22:07
  • When replacing multiple item in a string and want to make sure your replacements are unique. So the following will work : sentence.Replace("I was watching", "you were watching").Replace("you were playing", "I was playing") Commented Nov 19, 2019 at 22:16
  • This is a very interesting question. With code you introduce a third variable, but this is a string. Commented Nov 19, 2019 at 22:20
  • 1
    You could get the indices for each replacement location and then substring it. Commented Nov 19, 2019 at 22:23
  • see: stackoverflow.com/questions/49164968/… Commented Nov 20, 2019 at 0:03

3 Answers 3

2

You may leverage a MatchEvaluator as the replacement argument in Regex.Replace:

var sentence = "I was watching TV last night while she was reading a book and you were playing video games";
var result = Regex.Replace(sentence, @"\b(?:(I was)|you were)\b", m =>
    m.Groups[1].Success ? "you were" : "I was");
Console.WriteLine(result); 
// => you were watching TV last night while she was reading a book and I was playing video games

See the C# demo.

Here, \b(?:(I was)|you were)\b matches either I was (while capturing the match into Group 1) or you were as whole words (\b is a word boundary). If m.Groups[1].Success is true, the replacement is you were, else, it is I was.

Sign up to request clarification or add additional context in comments.

1 Comment

awesome! a simple and elegant solution
0

Another solution is to create two functions, one to replace the first occurence and the second to replace the last occurence :

public static string ReplaceFirstOccurrence (string Source, string Find, string Replace)
{
    int Place = Source.IndexOf(Find);
    string result = Source.Remove(Place, Find.Length).Insert(Place, Replace);
    return result;
}

public static string ReplaceLastOccurrence(string Source, string Find, string Replace)
{
    int Place = Source.LastIndexOf(Find);
    string result = Source.Remove(Place, Find.Length).Insert(Place, Replace);
    return result;
}
ReplaceFirstOccurrence(sentence, "I was", "you were");
ReplaceLastOccurrence(sentence, "you were", "I was");

Comments

0

This is very crude, would be slow for large strings, makes many memory allocations, but it's easy to explain and does the job.

var input = "I was watching TV last night while she was reading a book and you were playing video games";

var pairs = new [] { 
    (From: "I was", To: "you were"),
    (From: "you were", To:"I was")
    };

var s = input;
for(int i = 0; i < pairs.Length; i++) s = s.Replace(pairs[i].From, $"{{TOKEN{i}}}", ignoreCase: true, CultureInfo.InvariantCulture);
for(int i = 0; i < pairs.Length; i++) s = s.Replace($"{{TOKEN{i}}}", pairs[i].To);

Console.WriteLine(input);
Console.WriteLine(s);
s = char.ToUpper(s[0]) + s.Substring(1);
Console.WriteLine(s);

Output

I was watching TV last night while she was reading a book and you were playing video games
you were watching TV last night while she was reading a book and I was playing video games
You were watching TV last night while she was reading a book and I was playing video games

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.