CompareInfo.IndexOf メソッド (String, Char, CompareOptions)
指定した CompareOptions 値を使用して、指定した文字を検索し、検索対象文字列全体内でその文字が最初に出現する位置の 0 から始まるインデックス番号を返します。
Overloads Public Overridable Function IndexOf( _
ByVal source As String, _ ByVal value As Char, _ ByVal options As CompareOptions _) As Integer
[C#]
public virtual int IndexOf(stringsource,charvalue,CompareOptionsoptions);
[C++]
public: virtual int IndexOf(String* source,__wchar_tvalue,CompareOptionsoptions);
[JScript]
public function IndexOf(
source : String,value : Char,options : CompareOptions) : int;
パラメータ
- source
検索対象の文字列。 - value
source 内で検索する文字。 - options
文字列の比較方法を定義する CompareOptions 値。
戻り値
指定した CompareOptions 値を使用して、source 全体内で value が見つかった場合は、最初に見つかった位置の 0 から始まるインデックス番号。それ以外の場合は -1。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | source が null 参照 (Visual Basic では Nothing) です。 |
ArgumentException | options が有効な CompareOptions 値ではありません。 |
解説
検索範囲の文字列内で、検索はその文字列の先頭から開始して順方向に進み、その文字列の末尾で終了します。
CompareOptions.StringSort 値は、このメソッドに対して有効ではありません。
options に Ordinal 値が含まれていない場合、このオーバーロードは、カルチャを考慮した検索を実行します。カルチャによっては、char が合字の 'Æ' (U+00C6) など構成済み文字を表す Unicode 値の場合に、"AE" (U+0041、U+0045) のように正しい順序で出現した構成要素と等価であると見なします。 options に Ordinal 値が含まれている場合、このオーバーロードは番号順 (カルチャを考慮しない) 検索を実行します。この検索では、Unicode 値が同じ場合にだけ、ある char と別の char が等価であると見なします。char を検索する String.IndexOf のオーバーロードは、番号順検索を実行します。文字列を検索するオーバーロードは、カルチャを考慮した検索を実行します。
使用例
[Visual Basic, C#, C++] 次に示すのは、文字列内の最初と最後に出現する文字または部分文字列のインデックスを調べるコード例です。
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic
Public Class SamplesCompareInfo
Public Shared Sub Main()
' Creates CompareInfo for the InvariantCulture.
Dim myComp As CompareInfo = CultureInfo.InvariantCulture.CompareInfo
' Searches for the ligature .
Dim myStr As [String] = "Is AE or ae the same as or ?"
Console.WriteLine()
Console.WriteLine("No options : {0}", myStr)
PrintMarker(" AE : ", myComp.IndexOf(myStr, "AE"), myComp.LastIndexOf(myStr, "AE"))
PrintMarker(" ae : ", myComp.IndexOf(myStr, "ae"), myComp.LastIndexOf(myStr, "ae"))
PrintMarker(" : ", myComp.IndexOf(myStr, ""c), myComp.LastIndexOf(myStr, ""c))
PrintMarker(" : ", myComp.IndexOf(myStr, ""c), myComp.LastIndexOf(myStr, ""c))
Console.WriteLine("Ordinal : {0}", myStr)
PrintMarker(" AE : ", myComp.IndexOf(myStr, "AE", CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "AE", CompareOptions.Ordinal))
PrintMarker(" ae : ", myComp.IndexOf(myStr, "ae", CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ae", CompareOptions.Ordinal))
PrintMarker(" : ", myComp.IndexOf(myStr, ""c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, ""c, CompareOptions.Ordinal))
PrintMarker(" : ", myComp.IndexOf(myStr, ""c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, ""c, CompareOptions.Ordinal))
Console.WriteLine("IgnoreCase : {0}", myStr)
PrintMarker(" AE : ", myComp.IndexOf(myStr, "AE", CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "AE", CompareOptions.IgnoreCase))
PrintMarker(" ae : ", myComp.IndexOf(myStr, "ae", CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ae", CompareOptions.IgnoreCase))
PrintMarker(" : ", myComp.IndexOf(myStr, ""c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, ""c, CompareOptions.IgnoreCase))
PrintMarker(" : ", myComp.IndexOf(myStr, ""c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, ""c, CompareOptions.IgnoreCase))
' Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
myStr = "Is " & ChrW(&H0055) & ChrW(&H0308) & " or " & ChrW(&H0075) & ChrW(&H0308) & " the same as " & ChrW(&H00DC) & " or " & ChrW(&H00FC) & "?"
Console.WriteLine()
Console.WriteLine("No options : {0}", myStr)
PrintMarker(" U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308)), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308)))
PrintMarker(" u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308)), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308)))
PrintMarker(" : ", myComp.IndexOf(myStr, ""c), myComp.LastIndexOf(myStr, ""c))
PrintMarker(" : ", myComp.IndexOf(myStr, ""c), myComp.LastIndexOf(myStr, ""c))
Console.WriteLine("Ordinal : {0}", myStr)
PrintMarker(" U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.Ordinal))
PrintMarker(" u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.Ordinal))
PrintMarker(" : ", myComp.IndexOf(myStr, ""c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, ""c, CompareOptions.Ordinal))
PrintMarker(" : ", myComp.IndexOf(myStr, ""c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, ""c, CompareOptions.Ordinal))
Console.WriteLine("IgnoreCase : {0}", myStr)
PrintMarker(" U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.IgnoreCase))
PrintMarker(" u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.IgnoreCase))
PrintMarker(" : ", myComp.IndexOf(myStr, ""c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, ""c, CompareOptions.IgnoreCase))
PrintMarker(" : ", myComp.IndexOf(myStr, ""c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, ""c, CompareOptions.IgnoreCase))
End Sub 'Main
Public Shared Sub PrintMarker(Prefix As [String], First As Integer, Last As Integer)
' Determines the size of the array to create.
Dim mySize As Integer
If Last > First Then
mySize = Last
Else
mySize = First
End If
If mySize > - 1 Then
' Creates an array of Char to hold the markers.
Dim myCharArr(mySize + 1) As [Char]
' Inserts the appropriate markers.
If First > - 1 Then
myCharArr(First) = "f"c
End If
If Last > - 1 Then
myCharArr(Last) = "l"c
End If
If First = Last Then
myCharArr(First) = "b"c
End If
' Displays the array of Char as a String.
Console.WriteLine("{0}{1}", Prefix, New [String](myCharArr))
Else
Console.WriteLine(Prefix)
End If
End Sub 'PrintMarker
End Class 'SamplesCompareInfo
'This code produces the following output.
'
'No options : Is AE or ae the same as or ?
' AE : f l
' ae : f l
' : f l
' : f l
'Ordinal : Is AE or ae the same as or ?
' AE : b
' ae : b
' : b
' : b
'IgnoreCase : Is AE or ae the same as or ?
' AE : f l
' ae : f l
' : f l
' : f l
'
'No options : Is U" or u" the same as or ?
' U" : f l
' u" : f l
' : f l
' : f l
'Ordinal : Is U" or u" the same as or ?
' U" : b
' u" : b
' : b
' : b
'IgnoreCase : Is U" or u" the same as or ?
' U" : f l
' u" : f l
' : f l
' : f l
[C#]
using System;
using System.Globalization;
public class SamplesCompareInfo {
public static void Main() {
// Creates CompareInfo for the InvariantCulture.
CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;
// Searches for the ligature .
String myStr = "Is AE or ae the same as or ?";
Console.WriteLine();
Console.WriteLine( "No options : {0}", myStr );
PrintMarker( " AE : ", myComp.IndexOf( myStr, "AE" ), myComp.LastIndexOf( myStr, "AE" ) );
PrintMarker( " ae : ", myComp.IndexOf( myStr, "ae" ), myComp.LastIndexOf( myStr, "ae" ) );
PrintMarker( " : ", myComp.IndexOf( myStr, '' ), myComp.LastIndexOf( myStr, '' ) );
PrintMarker( " : ", myComp.IndexOf( myStr, '' ), myComp.LastIndexOf( myStr, '' ) );
Console.WriteLine( "Ordinal : {0}", myStr );
PrintMarker( " AE : ", myComp.IndexOf( myStr, "AE", CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "AE", CompareOptions.Ordinal ) );
PrintMarker( " ae : ", myComp.IndexOf( myStr, "ae", CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "ae", CompareOptions.Ordinal ) );
PrintMarker( " : ", myComp.IndexOf( myStr, '', CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, '', CompareOptions.Ordinal ) );
PrintMarker( " : ", myComp.IndexOf( myStr, '', CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, '', CompareOptions.Ordinal ) );
Console.WriteLine( "IgnoreCase : {0}", myStr );
PrintMarker( " AE : ", myComp.IndexOf( myStr, "AE", CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "AE", CompareOptions.IgnoreCase ) );
PrintMarker( " ae : ", myComp.IndexOf( myStr, "ae", CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "ae", CompareOptions.IgnoreCase ) );
PrintMarker( " : ", myComp.IndexOf( myStr, '', CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, '', CompareOptions.IgnoreCase ) );
PrintMarker( " : ", myComp.IndexOf( myStr, '', CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, '', CompareOptions.IgnoreCase ) );
// Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
myStr = "Is \u0055\u0308 or \u0075\u0308 the same as \u00DC or \u00FC?";
Console.WriteLine();
Console.WriteLine( "No options : {0}", myStr );
PrintMarker( " U\u0308 : ", myComp.IndexOf( myStr, "U\u0308" ), myComp.LastIndexOf( myStr, "U\u0308" ) );
PrintMarker( " u\u0308 : ", myComp.IndexOf( myStr, "u\u0308" ), myComp.LastIndexOf( myStr, "u\u0308" ) );
PrintMarker( " : ", myComp.IndexOf( myStr, '' ), myComp.LastIndexOf( myStr, '' ) );
PrintMarker( " : ", myComp.IndexOf( myStr, '' ), myComp.LastIndexOf( myStr, '' ) );
Console.WriteLine( "Ordinal : {0}", myStr );
PrintMarker( " U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "U\u0308", CompareOptions.Ordinal ) );
PrintMarker( " u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "u\u0308", CompareOptions.Ordinal ) );
PrintMarker( " : ", myComp.IndexOf( myStr, '', CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, '', CompareOptions.Ordinal ) );
PrintMarker( " : ", myComp.IndexOf( myStr, '', CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, '', CompareOptions.Ordinal ) );
Console.WriteLine( "IgnoreCase : {0}", myStr );
PrintMarker( " U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "U\u0308", CompareOptions.IgnoreCase ) );
PrintMarker( " u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "u\u0308", CompareOptions.IgnoreCase ) );
PrintMarker( " : ", myComp.IndexOf( myStr, '', CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, '', CompareOptions.IgnoreCase ) );
PrintMarker( " : ", myComp.IndexOf( myStr, '', CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, '', CompareOptions.IgnoreCase ) );
}
public static void PrintMarker( String Prefix, int First, int Last ) {
// Determines the size of the array to create.
int mySize;
if ( Last > First )
mySize = Last;
else
mySize = First;
if ( mySize > -1 ) {
// Creates an array of Char to hold the markers.
Char[] myCharArr = new Char[mySize+1];
// Inserts the appropriate markers.
if ( First > -1 )
myCharArr[First] = 'f';
if ( Last > -1 )
myCharArr[Last] = 'l';
if ( First == Last )
myCharArr[First] = 'b';
// Displays the array of Char as a String.
Console.WriteLine( "{0}{1}", Prefix, new String( myCharArr ) );
}
else
Console.WriteLine( Prefix );
}
}
/*
This code produces the following output.
No options : Is AE or ae the same as or ?
AE : f l
ae : f l
: f l
: f l
Ordinal : Is AE or ae the same as or ?
AE : b
ae : b
: b
: b
IgnoreCase : Is AE or ae the same as or ?
AE : f l
ae : f l
: f l
: f l
No options : Is U" or u" the same as or ?
U" : f l
u" : f l
: f l
: f l
Ordinal : Is U" or u" the same as or ?
U" : b
u" : b
: b
: b
IgnoreCase : Is U" or u" the same as or ?
U" : f l
u" : f l
: f l
: f l
*/
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
void PrintMarker( String* Prefix, int First, int Last ) {
// Determines the size of the array to create.
int mySize;
if ( Last > First )
mySize = Last;
else
mySize = First;
if ( mySize > -1 ) {
// Creates an array of Char to hold the markers.
Char myCharArr[] = new Char[mySize+1];
// Inserts the appropriate markers.
if ( First > -1 )
myCharArr[First] = 'f';
if ( Last > -1 )
myCharArr[Last] = 'l';
if ( First == Last )
myCharArr[First] = 'b';
// Displays the array of Char as a String.
Console::WriteLine( S"{0}{1}", Prefix, new String( myCharArr ) );
}
else
Console::WriteLine( Prefix );
}
int main() {
// Creates CompareInfo for the InvariantCulture.
CompareInfo* myComp = CultureInfo::InvariantCulture->CompareInfo;
// Searches for the ligature .
String* myStr = S"Is AE or ae the same as or ?";
Console::WriteLine();
Console::WriteLine( S"No options : {0}", myStr );
PrintMarker( S" AE : ", myComp->IndexOf( myStr, S"AE" ), myComp->LastIndexOf( myStr, S"AE" ) );
PrintMarker( S" ae : ", myComp->IndexOf( myStr, S"ae" ), myComp->LastIndexOf( myStr, S"ae" ) );
PrintMarker( S" : ", myComp->IndexOf( myStr, L'' ), myComp->LastIndexOf( myStr, L'' ) );
PrintMarker( S" : ", myComp->IndexOf( myStr, L'' ), myComp->LastIndexOf( myStr, L'' ) );
Console::WriteLine( S"Ordinal : {0}", myStr );
PrintMarker( S" AE : ", myComp->IndexOf( myStr, S"AE", CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, S"AE", CompareOptions::Ordinal ) );
PrintMarker( S" ae : ", myComp->IndexOf( myStr, S"ae", CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, S"ae", CompareOptions::Ordinal ) );
PrintMarker( S" : ", myComp->IndexOf( myStr, L'', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'', CompareOptions::Ordinal ) );
PrintMarker( S" : ", myComp->IndexOf( myStr, L'', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'', CompareOptions::Ordinal ) );
Console::WriteLine( S"IgnoreCase : {0}", myStr );
PrintMarker( S" AE : ", myComp->IndexOf( myStr, S"AE", CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, S"AE", CompareOptions::IgnoreCase ) );
PrintMarker( S" ae : ", myComp->IndexOf( myStr, S"ae", CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, S"ae", CompareOptions::IgnoreCase ) );
PrintMarker( S" : ", myComp->IndexOf( myStr, L'', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'', CompareOptions::IgnoreCase ) );
PrintMarker( S" : ", myComp->IndexOf( myStr, L'', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'', CompareOptions::IgnoreCase ) );
// Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
myStr = S"Is U\u0308 or u\u0308 the same as \u00DC or \u00FC?";
Console::WriteLine();
Console::WriteLine( S"No options : {0}", myStr );
PrintMarker( S" U\u0308 : ", myComp->IndexOf( myStr, S"U\u0308" ), myComp->LastIndexOf( myStr, S"U\u0308" ) );
PrintMarker( S" u\u0308 : ", myComp->IndexOf( myStr, S"u\u0308" ), myComp->LastIndexOf( myStr, S"u\u0308" ) );
PrintMarker( S" : ", myComp->IndexOf( myStr, L'' ), myComp->LastIndexOf( myStr, L'' ) );
PrintMarker( S" : ", myComp->IndexOf( myStr, L'' ), myComp->LastIndexOf( myStr, L'' ) );
Console::WriteLine( S"Ordinal : {0}", myStr );
PrintMarker( S" U\u0308 : ", myComp->IndexOf( myStr, S"U\u0308", CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, S"U\u0308", CompareOptions::Ordinal ) );
PrintMarker( S" u\u0308 : ", myComp->IndexOf( myStr, S"u\u0308", CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, S"u\u0308", CompareOptions::Ordinal ) );
PrintMarker( S" : ", myComp->IndexOf( myStr, L'', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'', CompareOptions::Ordinal ) );
PrintMarker( S" : ", myComp->IndexOf( myStr, L'', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'', CompareOptions::Ordinal ) );
Console::WriteLine( S"IgnoreCase : {0}", myStr );
PrintMarker( S" U\u0308 : ", myComp->IndexOf( myStr, S"U\u0308", CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, S"U\u0308", CompareOptions::IgnoreCase ) );
PrintMarker( S" u\u0308 : ", myComp->IndexOf( myStr, S"u\u0308", CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, S"u\u0308", CompareOptions::IgnoreCase ) );
PrintMarker( S" : ", myComp->IndexOf( myStr, L'', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'', CompareOptions::IgnoreCase ) );
PrintMarker( S" : ", myComp->IndexOf( myStr, L'', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'', CompareOptions::IgnoreCase ) );
}
/*
This code produces the following output.
No options : Is AE or ae the same as or ?
AE : f l
ae : f l
: f l
: f l
Ordinal : Is AE or ae the same as or ?
AE : b
ae : b
: b
: b
IgnoreCase : Is AE or ae the same as or ?
AE : f l
ae : f l
: f l
: f l
No options : Is U" or u" the same as or ?
U" : f l
u" : f l
: f l
: f l
Ordinal : Is U" or u" the same as or ?
U" : b
u" : b
: b
: b
IgnoreCase : Is U" or u" the same as or ?
U" : f l
u" : f l
: f l
: f l
*/
[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
参照
CompareInfo クラス | CompareInfo メンバ | System.Globalization 名前空間 | CompareInfo.IndexOf オーバーロードの一覧 | LastIndexOf | CompareOptions