IndexOf()

noun

say it: “IN-deks-uv” /ˈɪndɛksəv/

A string method that returns the position where a character or piece of text is found, or -1 if it is not there.

What is IndexOf() in C#?

IndexOf() — A string method that finds a character or piece of text and returns its position, counting from 0. It returns -1 when the text isn't there, so check for that before you use the result.

string email = "jordan@books.com";
Console.WriteLine(email.IndexOf('@'));    // 6
Console.WriteLine(email.IndexOf("shop")); // -1

In a sentence

  • “I used IndexOf to find the @ in the email address.”

Level: Apprentice · More C# terms at this level

Updated