IndexOf 方法 (Char, Int32, Int32)
 本文章是由機器翻譯。 將指標移到文章內的文字上方即可查看原文。 其他資訊。 譯文 原文

# String.IndexOf 方法 (Char, Int32, Int32)

.NET Framework 4.6 and 4.5

## 語法

public int IndexOf(
char value,
int startIndex,
int count
)

value

startIndex

count

## 例外狀況

ArgumentOutOfRangeException

countstartIndex 為負值。

-或-

startIndex 大於這個字串的長度。

-或-

count 大於這個字串的長度減去 startIndex

## 範例

// Example for the String.IndexOf( char, int, int ) method.
using System;

class IndexOfCII
{
public static void Main()
{
string br1 =
"0----+----1----+----2----+----3----+----" +
"4----+----5----+----6----+----7";
string br2 =
"0123456789012345678901234567890123456789" +
"0123456789012345678901234567890";
string str =
"ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi " +
"ABCDEFGHI abcdefghi ABCDEFGHI";

Console.WriteLine(
"This example of String.IndexOf( char, int, int )\n" +
"generates the following output." );
Console.WriteLine(
"{0}{1}{0}{2}{0}{3}{0}",
Environment.NewLine, br1, br2, str );

FindAllChar( 'A', str );
FindAllChar( 'a', str );
FindAllChar( 'I', str );
FindAllChar( 'i', str );
FindAllChar( '@', str );
FindAllChar( ' ', str );
}

static void FindAllChar( Char target, String searched )
{
Console.Write(
"The character '{0}' occurs at position(s): ",
target );

int     startIndex = -1;
int     hitCount = 0;

// Search for all occurrences of the target.
while( true )
{
startIndex = searched.IndexOf(
target, startIndex + 1,
searched.Length - startIndex - 1 );

if( startIndex < 0 )
break;

Console.Write( "{0}, ", startIndex );
hitCount++;
}

Console.WriteLine( "occurrences: {0}", hitCount );
}
}

/*
This example of String.IndexOf( char, int, int )
generates the following output.

0----+----1----+----2----+----3----+----4----+----5----+----6----+----7
01234567890123456789012345678901234567890123456789012345678901234567890
ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi ABCDEFGHI

The character 'A' occurs at position(s): 0, 20, 40, 60, occurrences: 4
The character 'a' occurs at position(s): 10, 30, 50, occurrences: 3
The character 'I' occurs at position(s): 8, 28, 48, 68, occurrences: 4
The character 'i' occurs at position(s): 18, 38, 58, occurrences: 3
The character '@' occurs at position(s): occurrences: 0
The character ' ' occurs at position(s): 9, 19, 29, 39, 49, 59, occurrences: 6
*/

## 平台

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (不支援伺服器核心角色), Windows Server 2008 R2 (SP1 (含) 以後版本支援伺服器核心角色，不支援 Itanium)

.NET Framework 並不支援各種平台的所有版本。如需支援版本的清單，請參閱.NET Framework 系統需求

## 社群新增項目

Microsoft 正展開一份線上問卷調查，了解您對於 MSDN 網站的看法。 如果您選擇參加，您離開 MSDN 網站時即會顯示線上問卷調查。