c# IndexOf

// To find the index of the first substring in a string use
// 'IndexOf()'
string str = "Hello World!"
str.IndexOf("o"); // Output: 4

// You can give a starting index and a length to search at
str.IndexOf("o", 6, 4); // Output: 7

// To find the last instance of a substring use 'LastIndexOf()'
str.LastIndexOf("o"); // Output: 7

3.86
7

                                    string word = "Hello";
int where = word.IndexOf("e");
// int where returns index number where the letter "e" is located.
// --0-1-2-3-4-- < Index number
// --H-e-l-l-o-- < Word
// In this case, the letter "e" is located in the index number 1

3.86 (7 Votes)
0
4.33
6
Wildmugle 85 points

                                    string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+---";
string br2 = "012345678901234567890123456789012345678901234567890123456789012345678";
string str = "Now is the time for all good men to come to the aid of their country.";
int start;
int at;
int end;
int count;

end = str.Length;
start = end/2;
Console.WriteLine();
Console.WriteLine("All occurrences of 'he' from position {0} to {1}.", start, end-1);
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
Console.Write("The string 'he' occurs at position(s): ");

count = 0;
at = 0;
while((start <= end) && (at > -1))
{
    // start+count must be a position within -str-.
    count = end - start;
    at = str.IndexOf("he", start, count);
    if (at == -1) break;
    Console.Write("{0} ", at);
    start = at+1;
}
Console.WriteLine();

/*
This example produces the following results:

All occurrences of 'he' from position 34 to 68.
0----+----1----+----2----+----3----+----4----+----5----+----6----+---
012345678901234567890123456789012345678901234567890123456789012345678
Now is the time for all good men to come to the aid of their country.

The string 'he' occurs at position(s): 45 56

*/

4.33 (6 Votes)
0
4.67
6
Hudhud 130 points

                                    int index = String.IndexOf(char x);
int index = String.IndexOf(string x);

4.67 (6 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
string substring c# index og c# indexof string example return of indexof in c# indexof c# means C# get indexof what is IndexOf c# c# index substring getting substring by index c# explain of index in c# .IndexOf( c# c# string indexof from end indexof == -1 C# indexof int [] c# what does indexof mean in c# c# 7 string.indexof substring C# get substring index of int index of c# string c# indexof from end Indexof position c# get index of c# get string specific indexof c# string.index of c# indexof c# string example string.indexOf() c# .NET string index of C# indexof substring c# indexof example indexof int c# c# indexof all indexof c# list get index that substring c# c# indexof. indexof syntax in c# string C# IndexOf c# char[] indexof c# IndexOf text indexof value -1 c# c# indexof return C# create indexOf how to get substring index in c# c# string get all indexes of substring c# string substring indexof c# string indexof all c# get substring with index string.indexof c# return value implement index of char in string c# algorithm index of char in string c# algorithm c# IndexOf(' ') substring and indexof in c# indexof function in c# c# find in string position csharp string indexof How to check the index of a letter in a string in C# get indexOf each specific character in and string c# c# get first occurrence in string string.indexof c# get idex of the first character after an index c# c sharp substring indexof how to find a char in string c# .IndexOf C# str.IndexOf(""); c# comand to find first occurence asp core c# string contains start index substring c# indexof indexof to grab first letter c# how to check an index of a string C# get index string c# c# find symbol in string string index of char count c# asp.net indexof include special characters get index of substring c# how to find a spesifik letter in a string c# index of for string c# search string using index of asp.net c# string at index indexof c# example check string index is number c# C sharp chareter index csharp string charetec index how to get the index of a letter in a string in c# c# index string c# indexof c# find chars in string find char in string c# return index of letter in string c# how to return an idex of a string in c# c# search a string for a substring substring indexof c# c# display value of the inde check string contains with indexof in c# c# find string in a string c# string[] with index string C# string index of / position of character in string c# index of string in c# powershell indexof string index of character in a string C# how to get an index of a string c# c# search string c# indexof() use indexof and lastindexof c# in hebrew string c# strpos example indexOf() in C# string find c# using indexof asp.net c# csharp indexof find character from string c# how to find a character of a string in c# c# index of charcter in a string c# Find the first location from the end of the string find the first location from the end of the string c# Find Index string C# get a substring AND INDEXOF from a string CSHARP c# substring indexing string.indexof how to find something below the starting index c# string.indexof how to find something below the starting index find first index of character c# how to find starting index given string in the string in c# strinng.indexOf c# string first index c# indexof string c# indexof tring c# c# string scan position index of method c string.indexof .net c# find index in string c# index of a string of names c# find position of character in string find index of character C# string c# string position of substring c# find index of a string in a string index of a string [] c# how to get string index in substring in C# POSTITION OF STRING C# get value from substring indexof c# c# string position to char indexOf in string .net String index C# meaning C# find character in string indexof in c# get start index of substring c# how to get first index of _ in c# .next string substring first index of c# INDEX OF first caracter c# string first index of string c# find index of element in string c# find index in string c# indexof exception c# c# find a character in string index of string c# find index of string c# find a character in a string c# c# index of example c# find index of character in string C# str pos write a method to find character in a string c# find character in string in c# C# Indexof() syntax indexof c# string c# find substring c sharp index of a string c# find string pos in string index of a char in a string c# get string position c# c# find character in string\ search string in text c# c# search string method c# string find index of char how to find the index of line in c# c# string first index of string start from which position in c# first first index of work in string c# firstindexof c# c# substring indexof c# findindex of string c# find a character in a string c# how to find character in string c# indexof examples index in string c# index of a character in a string c# was ist ein IndexOf programming C# string indexof C# ThrowOverflowOrFormatException string indexof C# at System.Number.ThrowOverflowOrFormatException c# string indexof indexof in c# if not found c# find position of word in string find string index char c# find index of string specific character c# index of first instance find character in string c# c# get position of character in string c# position of character in string c# find index of string in string csharp index of string get ta string position in a string c# if string.indexof c# if line.indexof c# line.indexof c# how to find the position of a letter in a string C# c# find char in string how to make the index of a string a string c# c# string index of substring can i do IndexOf more than one character c# find first occurrence of a string in another string c# c# find string in another string string.indeof C# find character ins tring C# c# find index of char in string c# index of with strings index of in c# index of char c# c# index of string c# find position of the 2 character in string how to use indexof in c# c# find x in string find string inside a string c# get the index of a specific char c# string indexof c# c# indexof char in string if not found c# indexof char in string c# position in string x# indexOf get index of character in string c# get index of char in string cs return position os a character in a string c# find index of a character in a string c# c# get location of string in string c# string.indexof c# readline indexof c# string find character find character index of string c# C# string substring(3, str.IndexOf(",") c# index of character in string string index of c# index of c# find letter in c# string c# string position how to find index of character in string c# string index c# get index of char in string c# finding a character using index c# c# get index of char in char search in string from current index c# finding an index of a string c# c# compare string index with char index first index of c# what does the .indexof c# do return first string found c# how to find '\' in string c# indexof c# string index of method c# find the indext of a character in a string c# indexof() c# c# indexof string c# index of char that is not in string c# find location of character in string c# string index c sharp index of substring
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source