c# IndexOf string search I have this code working but its using IndexOf I am not sure if that is the best or only option for search for a string value. What I have is a list that will need to remove a world from the child name if it is the same as the name.


  Is this the best way to complete this task?


namespace DupName
{
    public class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sbchildName = new StringBuilder();

            List&lt;Item&gt; items = new List&lt;Item&gt;()
            .AddAlso(new Item { Id = 1, Name = "Audi", ChildName = "Audi A3 Premium Plus" })
            .AddAlso(new Item { Id = 2, Name = "Audi", ChildName = "Audi A5 Premium Plus" })
            .AddAlso(new Item { Id = 1, Name = "Audi", ChildName = "Audi A6 Premium Plus 55 TFSI quattro" })
            .AddAlso(new Item { Id = 2, Name = "Audi", ChildName = "S5 Premium Plus 3.0 TFSI quattro" })
            .AddAlso(new Item { Id = 2, Name = "Audi", ChildName = "S3 Premium Plus 3.0 TFSI quattro" });

        foreach (var item in items)
        {
            if (item.ChildName.IndexOf(item.Name) != -1)
            {
                string temp = string.Format("{0} {1}", item.ChildName.Replace(item.Name, ""), "Car");
                Console.WriteLine(temp.Trim());
                childName.AppendFormat(temp.Trim());
            }
            else
            {
                string temp = string.Format("{0} {1}", item.ChildName, "Car");
                Console.WriteLine(temp.Trim());
                childName.AppendFormat(temp.Trim());
            }
        }
    }

    public static class Extensions
    {
        public static List&lt;T&gt; AddAlso&lt;T&gt;(this List&lt;T&gt; list, T item)
        {
            list.Add(item);
            return list;
        }
    }

    public class Item
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string ChildName { get; set; }
    }
}


output:

A3 Premium Plus Car
A5 Premium Plus Car
A6 Premium Plus 55 TFSI quattro Car
S5 Premium Plus 3.0 TFSI quattro Car
S3 Premium Plus 3.0 TFSI quattro Car
Because you use the returned value of IndexOf() only to check if it is &gt; -1 a more readable way would be to use Contains() instead of IndexOf().   

Internally Contains() does mostly the same than your comparison of the returned value. In the reference source of Contains() you will find  

[Pure]
public bool Contains( string value ) {
    return ( IndexOf(value, StringComparison.Ordinal) &gt;=0 );
}  


while the counterpart of IndexOf() will look like so  

[Pure]
public int IndexOf(String value) {
    return IndexOf(value, StringComparison.CurrentCulture);
}  


The difference between these two methods by the meaning of the internally called IndexOf() method is the passed StringComparison parameter.  

By reading the documentation you will read in the remarks section  


  When you call a string comparison method such as String.Compare,
  String.Equals, or String.IndexOf, you should always call an overload
  that includes a parameter of type StringComparison so that you can
  specify the type of comparison that the method performs. For more
  information, see Best Practices for Using Strings.  


Let us check what that means by using the german word daß and compare the results from StringComparison.CurrentCulture, StringComparison.InvariantCulture and StringComparison.Ordinal  

Console.WriteLine("daß".IndexOf("daß", StringComparison.Ordinal));
Console.WriteLine("daß".IndexOf("dass", StringComparison.Ordinal));
Console.WriteLine("daß".IndexOf("dass", StringComparison.CurrentCulture));
Console.WriteLine("daß".IndexOf("dass", StringComparison.InvariantCulture));


this will yield the results  


  0
  -1
  0
  0  


As you see you need to know what you want to get returned by calling IndexOf(). If you need to know if the exact string is contained inside another string you shouldn't use the default IndexOf() method.