Microsoft.Test.CommandLineParsing.Command.Name: Contains vs. EndsWith vs LastIndexOf

Jun 21, 2010 at 12:46 PM
Wouldn't this code: string typeName = this.GetType().Name; if (typeName.Contains("Command")) { return typeName.Remove(typeName.LastIndexOf("Command", StringComparison.Ordinal)); } else { return typeName; } be more correct like this: string typeName = this.GetType().Name; if (typeName.EndsWith("Command")) { return typeName.Remove(typeName.LastIndexOf("Command")); } else { return typeName; } and even better like this: string typeName = this.GetType().Name; int idx = typeName.LastIndexOf("Command"); if (idx > 0) { return typeName.Remove(idx); } else { return typeName; } ??? Both Contains and LastIndexOf perform comparisons and the same StringComparison value should be used.