指定文字がこのインスタンス内で最初に見つかった位置のインデックスをレポートします。検索は指定した文字位置から開始され、指定した数の文字位置が検査されます。
Overloads Public Function IndexOf( _
ByVal value As Char, _ ByVal startIndex As Integer, _ ByVal count As Integer _) As Integer
[C#]
public int IndexOf(charvalue,intstartIndex,intcount);
[C++]
public: int IndexOf(__wchar_tvalue,intstartIndex,intcount);
[JScript]
public function IndexOf(
value : Char,startIndex : int,count : int) : int;
パラメータ
- value
シークする Unicode 文字。 - startIndex
検索が開始される位置。 - count
検査する文字位置の数。
戻り値
その文字が見つかった場合は、value のインデックス位置。見つからなかった場合は -1。
例外
| 例外の種類 | 条件 |
|---|---|
| ArgumentOutOfRangeException | count または startIndex が負の値です。
または count + startIndex が指定する位置が、このインスタンスの末尾を越えています。 |
解説
startIndex から startIndex + count -1 番目の位置まで検索が実行されましたが、 startIndex + count の文字は検出されませんでした。
インデックスの番号付けは 0 から始まります。
value の検索では大文字と小文字が区別されます。
このメソッドは、序数 (カルチャに依存しない) 検索を実行します。この検索方法では、2 つの文字は Unicode スカラ値が等しいときだけ等価と見なされます。カルチャに依存した検索を実行するには、 CompareInfo.IndexOf メソッドを使用します。このメソッドを使用して検索すると、合字の "A" (U+00C6) のような構成済み文字を表す Unicode 値は、'AE' (U+0041, U+0045) のようにその文字の構成要素が正しい順序で出現した場合、これらの構成要素と (カルチャの種類に応じて) 等価と見なされます。
使用例
[Visual Basic, C#, C++] IndexOf メソッドの例を次に示します。
' Example for the String.IndexOf( Char, Integer, Integer ) method.
Imports System
Imports Microsoft.VisualBasic
Module IndexOfCII
Sub Main()
Dim br1 As String = _
"0----+----1----+----2----+----3----+----" & _
"4----+----5----+----6----+----7"
Dim br2 As String = _
"0123456789012345678901234567890123456789" & _
"0123456789012345678901234567890"
Dim str As String = _
"ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi " & _
"ABCDEFGHI abcdefghi ABCDEFGHI"
Console.WriteLine( _
"This example of String.IndexOf( Char, Integer, Integer )" & _
vbCrLf & "generates the following output." )
Console.WriteLine( _
"{0}{1}{0}{2}{0}{3}{0}", _
Environment.NewLine, br1, br2, str)
FindAllChar("A"c, str)
FindAllChar("a"c, str)
FindAllChar("I"c, str)
FindAllChar("i"c, str)
FindAllChar("@"c, str)
FindAllChar(" "c, str)
End Sub 'Main
Sub FindAllChar(target As Char, searched As String)
Console.Write( _
"The character ""{0}"" occurs at position(s): ", target)
Dim startIndex As Integer = - 1
Dim hitCount As Integer = 0
' Search for all occurrences of the target.
While True
startIndex = searched.IndexOf( _
target, startIndex + 1, _
searched.Length - startIndex - 1)
' Exit the loop if the target is not found.
If startIndex < 0 Then
Exit While
End If
Console.Write("{0}, ", startIndex)
hitCount += 1
End While
Console.WriteLine("occurrences: {0}", hitCount)
End Sub 'FindAllChar
End Module 'IndexOfCII
' This example of String.IndexOf( Char, Integer, Integer )
' 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
[C#]
// 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 );
// Exit the loop if the target is not found.
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
*/
[C++]
// Example for the String::IndexOf( Char, int, int ) method.
#using <mscorlib.dll>
using namespace System;
void FindAllChar( Char target, String* searched )
{
Console::Write(
S"The character '{0}' occurs at position(s): ",
__box( 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 );
// Exit the loop if the target is not found.
if( startIndex < 0 )
break;
Console::Write( S"{0}, ", __box( startIndex ) );
hitCount++;
}
Console::WriteLine( S"occurrences: {0}", __box( hitCount ) );
}
void main()
{
String* br1 =
S"0----+----1----+----2----+----3----+----"
S"4----+----5----+----6----+----7";
String* br2 =
S"0123456789012345678901234567890123456789"
S"0123456789012345678901234567890";
String* str =
S"ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi "
S"ABCDEFGHI abcdefghi ABCDEFGHI";
Console::WriteLine(
S"This example of String::IndexOf( Char, int, int )\n"
S"generates the following output." );
Console::WriteLine(
S"{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 );
}
/*
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
*/
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン
をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
参照
String クラス | String メンバ | System 名前空間 | String.IndexOf オーバーロードの一覧 | Char | Int32 | IndexOfAny | LastIndexOf | LastIndexOfAny