Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. string.Length / IndexOf()

string.Length / IndexOf()

Since: C# 1.0(2002)

The Length property returns the length of a string, and the IndexOf() method returns the position where a specified character or string first appears.

Syntax

string.Length

string.IndexOf(string value)
string.IndexOf(string value, int startIndex)

string.LastIndexOf(string value)

Member List

MemberDescription
LengthReturns the number of characters in the string as an integer. Returns 0 for an empty string.
IndexOf(string value)Returns the zero-based index of the first occurrence of the specified string. Returns -1 if not found.
IndexOf(string value, int startIndex)Searches starting at the specified startIndex and returns the index of the first occurrence found.
LastIndexOf(string value)Returns the index of the last occurrence of the specified string. Returns -1 if not found.

Sample Code

Program.cs
using System;

string greeting = "Hello, World!";

Console.WriteLine(greeting.Length); // 13

// Use IndexOf() to find the position of a substring
int pos = greeting.IndexOf("World");
Console.WriteLine(pos); // 7

// Returns -1 if the substring is not found
int notFound = greeting.IndexOf("xyz");
Console.WriteLine(notFound); // -1

This produces the following output:

dotnet run
13
7
-1

startIndex and LastIndexOf()

Specifying startIndex in IndexOf() starts the search from that position. LastIndexOf() returns the position of the last occurrence.

SearchSample.cs
using System;

// Use startIndex to search from a specific position
string path = "C:/users/akiba/documents/akiba.txt";
int first = path.IndexOf("akiba");
int next = path.IndexOf("akiba", first + 1);
Console.WriteLine(first); // 9
Console.WriteLine(next); // 25

// Use LastIndexOf() to find the last occurrence
string text = "abcabcabc";
Console.WriteLine(text.LastIndexOf("a")); // 6

// Case-insensitive search
string msg = "Hello, C# World!";
int idx = msg.IndexOf("hello", StringComparison.OrdinalIgnoreCase);
Console.WriteLine(idx); // 0

This produces the following output:

dotnet run
9
25
6
0

Practical Pattern: Splitting an Email Address

This example uses IndexOf() and Substring() together to split an email address into a username and a domain.

EmailParse.cs
using System;

string email = "okabe@wp-p.info";
int atIndex = email.IndexOf("@");
if (atIndex >= 0) {
	string user = email.Substring(0, atIndex);
	string domain = email.Substring(atIndex + 1);
	Console.WriteLine($"User: {user}"); // User: okabe
	Console.WriteLine($"Domain: {domain}"); // Domain: wp-p.info
} else {
	Console.WriteLine("Invalid email address.");
}

This produces the following output:

dotnet run
User: okabe
Domain: wp-p.info

Common Mistakes

Common Mistake: Calling Substring() Without Checking the IndexOf() Return Value

If IndexOf() returns -1 (not found) and you pass that value directly to Substring(), an ArgumentOutOfRangeException will be thrown.

using System;

// NG: Calling Substring() when IndexOf() returns -1 throws an exception
string text = "Hello, World!";
int pos = text.IndexOf("C#"); // Returns -1
string part = text.Substring(pos); // ArgumentOutOfRangeException

This produces the following output:

dotnet run
Unhandled exception. System.ArgumentOutOfRangeException: StartIndex cannot be less than zero.
using System;

// OK: Always check for -1 first
string text = "Hello, World!";
int pos = text.IndexOf("C#");
if (pos >= 0) {
	string part = text.Substring(pos);
	Console.WriteLine(part);
} else {
	Console.WriteLine("Not found.");
}

This produces the following output:

dotnet run
Not found.

Notes

Length is a property of the string (not a method), so no parentheses are needed when accessing it. In C#, string indices are zero-based, and each character — including multi-byte characters — counts as one.

IndexOf() is case-sensitive. To perform a case-insensitive search, use IndexOf(value, StringComparison.OrdinalIgnoreCase).

To extract or remove part of a string, see Substring() / Remove(). To replace characters or check for containment, see Replace() / Contains().

If you find any errors or copyright issues, please .