Whats is correct to do?
check if exists, then remove?
var input = "foo #main baa";
if(input.Contains("#main")) {
input = input.Replace("#main", "");
}
or just:
input = input.Replace("#main", "");
Well, this seem a simple question,but I really want know.
Thanks in advance.
The
Containscheck actually just makes your code slower.Remove it.
The
Containscall needs to loop through the string until it finds#main.The
Replacecall then needs to do the same exact loop (it can’t remember it from theContainscall).This is a Shlemiel the Painter’s algorithm.
Replacecan handle strings with zero or more occurrences of the search string, so you don’t need the check.