-2

Let's imagine we have this string:

string Transport = "car motorcycle motorcycle plane train";

How can I find the first occurence of the word "motorcycle" in this string and change it in the original string to something else instead of having to create a separate string using LINQ?

Itsme1
  • 75
  • 9
  • 3
    *"and change it in the original string"* -- strings in C# are immutable, and cannot be changed – canton7 Jul 21 '20 at 12:16
  • 1
    Transport = Transport.ReplaceFirstOccurence("motorcycle")? – Itsme1 Jul 21 '20 at 12:17
  • 5
    Why do people need LINQ for everything nowadays? Can't we just implement it in an ordinary way? – Thomas Weller Jul 21 '20 at 12:18
  • What have you tried so far? – d4zed Jul 21 '20 at 12:19
  • The main problem that I have is that I can find the first occurence of the word using LINQ, however, when the `Transport.Replace` method replaces all occurences of this word in the string instead of just one. – Itsme1 Jul 21 '20 at 12:21
  • 1
    Looks like you have to use `string.IndexOf` and `string.Substring` – canton7 Jul 21 '20 at 12:24
  • Find indexOf motorcycle get `string before it + "" + string after indexOf` – Prasad Telkikar Jul 21 '20 at 12:27
  • 4
    Does this answer your question? [How do I replace the \*first instance\* of a string in .NET?](https://stackoverflow.com/questions/141045/how-do-i-replace-the-first-instance-of-a-string-in-net) – SomeBody Jul 21 '20 at 12:28
  • It does not explain how to do it using LINQ, so no. – Itsme1 Jul 21 '20 at 12:30
  • 1
    Of that duplicate suggestion, please don't use a Regex solution. It's not needed. – Thomas Weller Jul 21 '20 at 12:30
  • You can change it without building a new string but, not building a new string is not always better. –  Jul 21 '20 at 12:34
  • 1
    Why does it "need to be in LINQ"? This seems like a totally arbitrary constraint. LINQ is just a pile of syntactical sugar over iterators. I can't think of a good reason why you would discount a perfectly good solution simply because it's not LINQ? This is a dupe for me, the fact that the duplicate does or does not use LINQ is irrelevant. – Liam Jul 21 '20 at 13:35
  • If you really want to use LINQ you could do `var result = string.Join("bob", transport.Split(new string[] { "motorcycle" }, 2, StringSplitOptions.None).Select(z => z));` I mean the LINQ usage is pointless (it does nothing useful). But it is there! – mjwills Jul 21 '20 at 13:59

2 Answers2

1
string Transport = "car motorcycle motorcycle plane train";
string word = "motorcycle";
string pattern = Regex.Escape(word);
        
var regex = new Regex(pattern);            
string result = regex.Replace(Transport, "something", 1);

Console.WriteLine(result);
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
0

This should do the trick. oldValue is the value you want to find and replace, newValue is the value you want to replace with.

static void Main(string[] args)
{
    string transport = "car motorcycle motorcycle plane train";
    transport = ReplaceFirst(transport, "motorcycle", "not motorcycle");
    Console.WriteLine(transport);
    // prints "car not motorcycle motorcylce plane train
}

static string ReplaceFirst(string values, string oldValue, string newValue)
{
    int index = values.IndexOf(oldValue);
    if (index == -1) return values;
    values = values.Substring(0, index)
        + newValue
        + values.Substring(index + oldValue.Length);
    return values;
}

I'm reading this question to mean that you don't want to use LINQ.*

madmonk46
  • 421
  • 3
  • 10