Does string.Replace("a", "b") automatically check if "a" exists?
c#, contains, replace, string
Solution
You just have to read msdn: ( or try it out yourself )
Return Value Type: System.String A string that is equivalent to the current string except that all instances of oldValue are replaced with newValue. If oldValue is not found in the current instance, the method returns the current instance unchanged.
Side-note: since strings are immutable(you cannot change the instance) you have to reassign a new string if you want to change the old:
banana = banana.Replace("apple", "pie");
Problem
``` string banana = "banana apple"; banana.Replace("apple", "pie"); ``` If I want to replace apple with pie, can I do it like that, or do I need to use the following? ``` if(banana.Contains("apple")) banana.Replace("apple", "pie"); ```