String.IndexOf メソッド (Char, Int32)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As String Dim value As Char Dim startIndex As Integer Dim returnValue As Integer returnValue = instance.IndexOf(value, startIndex)
戻り値
その文字が見つかった場合は、value のインデックス位置。見つからなかった場合は -1。


検索範囲は startIndex から文字列の末尾までです。
このメソッドは、序数 (カルチャに依存しない) 検索を実行します。この検索方法では、2 つの文字は Unicode スカラ値が等しいときだけ等価と見なされます。カルチャに依存した検索を実行するには、CompareInfo.IndexOf メソッドを使用します。このメソッドを使用して検索すると、合字の "A" (U+00C6) のような構成済み文字を表す Unicode 値は、'AE' (U+0041, U+0045) のようにその文字の構成要素が正しい順序で出現した場合、これらの構成要素と (カルチャの種類に応じて) 等価と見なされます。

' Sample for String.IndexOf(Char, Int32) Imports System Class Sample Public Shared Sub Main() Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-" Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456" Dim str As String = "Now is the time for all good men to come to the aid of their party." Dim start As Integer Dim at As Integer Console.WriteLine() Console.WriteLine("All occurrences of 't' from position 0 to {0}.", str.Length - 1) Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str) Console.Write("The letter 't' occurs at position(s): ") at = 0 start = 0 While start < str.Length AndAlso at > - 1 at = str.IndexOf("t"c, start) If at = - 1 Then Exit While End If Console.Write("{0} ", at) start = at + 1 End While Console.WriteLine() End Sub 'Main End Class 'Sample ' 'This example produces the following results: ' 'All occurrences of 't' from position 0 to 66. '0----+----1----+----2----+----3----+----4----+----5----+----6----+- '0123456789012345678901234567890123456789012345678901234567890123456 'Now is the time for all good men to come to the aid of their party. ' 'The letter 't' occurs at position(s): 7 11 33 41 44 55 64 ' '
// Sample for String.IndexOf(Char, Int32) using System; class Sample { public static void Main() { string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"; string br2 = "0123456789012345678901234567890123456789012345678901234567890123456"; string str = "Now is the time for all good men to come to the aid of their party."; int start; int at; Console.WriteLine(); Console.WriteLine("All occurrences of 't' from position 0 to {0}.", str.Length-1); Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str); Console.Write("The letter 't' occurs at position(s): "); at = 0; start = 0; while((start < str.Length) && (at > -1)) { at = str.IndexOf('t', start); if (at == -1) break; Console.Write("{0} ", at); start = at+1; } Console.WriteLine(); } } /* This example produces the following results: All occurrences of 't' from position 0 to 66. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. The letter 't' occurs at position(s): 7 11 33 41 44 55 64 */
// Sample for String::IndexOf(Char, Int32) using namespace System; int main() { String^ br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"; String^ br2 = "0123456789012345678901234567890123456789012345678901234567890123456"; String^ str = "Now is the time for all good men to come to the aid of their party."; int start; int at; Console::WriteLine(); Console::WriteLine( "All occurrences of 't' from position 0 to {0}.", str->Length - 1 ); Console::WriteLine( "{1}{0}{2}{0}{3}{0}", Environment::NewLine, br1, br2, str ); Console::Write( "The letter 't' occurs at position(s): " ); at = 0; start = 0; while ( (start < str->Length) && (at > -1) ) { at = str->IndexOf( 't', start ); if ( at == -1 ) break; Console::Write( "{0} ", at ); start = at + 1; } Console::WriteLine(); } /* This example produces the following results: All occurrences of 't' from position 0 to 66. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. The letter 't' occurs at position(s): 7 11 33 41 44 55 64 */
// Sample for String.IndexOf(Char, Int32) import System.*; class Sample { public static void main(String[] args) { String br1 = "0----+----1----+----2----+----3----+----4----+----5----" + "+----6----+-"; String br2 = "01234567890123456789012345678901234567890123456789012345" + "67890123456"; String str = "Now is the time for all good men to come to the aid of " + "their party."; int start; int at; Console.WriteLine(); Console.WriteLine("All occurrences of 't' from position 0 to {0}.", (Int32)(str.get_Length() - 1)); Console.Write("{1}{0}", Environment.get_NewLine(), br1); Console.Write("{1}{0}", Environment.get_NewLine(), br2); Console.WriteLine("{1}{0}", Environment.get_NewLine(), str); Console.Write("The letter 't' occurs at position(s): "); at = 0; start = 0; while (start < str.get_Length() && at > -1) { at = str.IndexOf('t', start); if (at == -1) { break; } Console.Write("{0} ", (Int32)at); start = at + 1; } Console.WriteLine(); } //main } //Sample /* This example produces the following results: All occurrences of 't' from position 0 to 66. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. The letter 't' occurs at position(s): 7 11 33 41 44 55 64 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.IndexOf メソッド


String.IndexOf メソッド (String, Int32)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As String Dim value As String Dim startIndex As Integer Dim returnValue As Integer returnValue = instance.IndexOf(value, startIndex)
戻り値
その文字列が見つかった場合は、value のインデックス位置。見つからなかった場合は -1。value が Empty の場合、戻り値は startIndex です。


このメソッドは、現在のカルチャを使用して、単語 (大文字/小文字を区別し、カルチャに依存した) 検索を実行します。このインスタンスの startIndex で指定される文字位置から最後の文字位置まで検索されます。

検索対象の文字列内で、指定した文字列をすべて検索するコード例を次に示します。
Imports System Public Class IndexOfTest Public Shared Sub Main() Dim strSource As String = "This is the string which we will perform the search on" Console.WriteLine("The search string is:{0}{1}{0}", Environment.NewLine, strSource) Dim strTarget As String = "" Dim found As Integer = 0 Dim totFinds As Integer = 0 Do Console.Write("Please enter a search value to look for in the above string (hit Enter to exit) ==> ") strTarget = Console.ReadLine() If strTarget <> "" Then Dim i As Integer For i = 0 To strSource.Length - 1 found = strSource.IndexOf(strTarget, i) If found > 0 Then totFinds += 1 i = found Else Exit For End If Next i Else Return End If Console.WriteLine("{0}The search parameter '{1}' was found {2} times.{0}", Environment.NewLine, strTarget, totFinds) totFinds = 0 Loop While True End Sub 'Main End Class 'IndexOfTest
using System; public class IndexOfTest { public static void Main() { string strSource = "This is the string which we will perform the search on"; Console.WriteLine("The search string is:{0}\"{1}\"{0}", Environment.NewLine, strSource); string strTarget = ""; int found = 0; int totFinds = 0; do { Console.Write("Please enter a search value to look for in the above string (hit Enter to exit) ==> "); strTarget = Console.ReadLine(); if (strTarget != "") { for (int i = 0; i < strSource.Length; i++) { found = strSource.IndexOf(strTarget, i); if (found > 0) { totFinds++; i = found; } else break; } } else return; Console.WriteLine("{0}The search parameter '{1}' was found {2} times.{0}" , Environment.NewLine, strTarget, totFinds); totFinds = 0; } while ( true ); } }
using namespace System; int main() { String^ strSource = "This is the string which we will perform the search on"; Console::WriteLine( "The search string is: {0}\"{1}\" {0}", Environment::NewLine, strSource ); String^ strTarget = ""; int found = 0; int totFinds = 0; do { Console::Write( "Please enter a search value to look for in the above string (hit Enter to exit) ==> " ); strTarget = Console::ReadLine(); if ( !strTarget->Equals( "" ) ) { for ( int i = 0; i < strSource->Length; i++ ) { found = strSource->IndexOf( strTarget, i ); if ( found > 0 ) { totFinds++; i = found; } else break; } } else return 0; Console::WriteLine( "{0}The search parameter '{1}' was found {2} times. {0}", Environment::NewLine, strTarget, totFinds ); totFinds = 0; } while ( true ); }
import System.*; public class IndexOfTest { public static void main(String[] args) { String strSource = "This is the string which we will perform the " + "search on"; Console.WriteLine("The search string is:{0}\"{1}\"{0}", Environment.get_NewLine(), strSource); String strTarget = ""; int found = 0; int totFinds = 0; do { Console.Write("Please enter a search value to look for in the " + "above string (hit Enter to exit) ==> "); strTarget = Console.ReadLine(); if (!strTarget.Equals("")) { for (int i = 0; i < strSource.get_Length(); i++) { found = strSource.IndexOf(strTarget, i); if (found > 0) { totFinds++; i = found; } else { break; } } } else { return; } Console.WriteLine("{0}The search parameter '{1}' was found {2} " + "times.{0}", Environment.get_NewLine(), strTarget, (Int32)totFinds); totFinds = 0; } while (true); } //main } //IndexOfTest
import System; public class IndexOfTest { public static function Main() : void { var strSource : String = "This is the string which we will perform the search on"; Console.WriteLine("The search string is:{0}\"{1}\"{0}", Environment.NewLine, strSource); var strTarget : String = ""; var found : int = 0; var totFinds : int = 0; do { Console.Write("Please enter a search value to look for in the above string (hit Enter to exit) ==> "); strTarget = Console.ReadLine(); if (strTarget != "") { for (var i : int = 0; i < strSource.Length; i++) { found = strSource.IndexOf(strTarget, i); if (found > 0) { totFinds++; i = found; } else break; } } else return; Console.WriteLine("{0}The search parameter '{1}' was found {2} times.{0}" , Environment.NewLine, strTarget, totFinds); totFinds = 0; } while ( true ); } } IndexOfTest.Main();

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.IndexOf メソッド (String, Int32, Int32)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Function IndexOf ( _ value As String, _ startIndex As Integer, _ count As Integer _ ) As Integer
Dim instance As String Dim value As String Dim startIndex As Integer Dim count As Integer Dim returnValue As Integer returnValue = instance.IndexOf(value, startIndex, count)
戻り値
その文字列が見つかった場合は、value のインデックス位置。見つからなかった場合は -1。value が Empty の場合、戻り値は startIndex です。


このメソッドは、現在のカルチャを使用して、単語 (大文字/小文字を区別し、カルチャに依存した) 検索を実行します。startIndex から startIndex + count -1 番目の位置まで検索が実行されましたが、startIndex + count の文字は検出されませんでした。

部分文字列から "he" という文字列の出現するインデックス位置をすべて検索するコード例を次に示します。検索を繰り返すごとに、検索対象の文字数を再計算する必要がある点に注意してください。
' Sample for String.IndexOf(String, Int32, Int32) Imports System Class Sample Public Shared Sub Main() Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-" Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456" Dim str As String = "Now is the time for all good men to come to the aid of their party." Dim start As Integer Dim at As Integer Dim [end] As Integer Dim count As Integer [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] AndAlso at > - 1 ' start+count must be a position within -str-. count = [end] - start at = str.IndexOf("he", start, count) If at = - 1 Then Exit While End If Console.Write("{0} ", at) start = at + 1 End While Console.WriteLine() End Sub 'Main End Class 'Sample ' 'This example produces the following results: ' 'All occurrences of 'he' from position 33 to 66. '0----+----1----+----2----+----3----+----4----+----5----+----6----+- '0123456789012345678901234567890123456789012345678901234567890123456 'Now is the time for all good men to come to the aid of their party. ' 'The string 'he' occurs at position(s): 45 56 ' '
// Sample for String.IndexOf(String, Int32, Int32) using System; class Sample { public static void Main() { string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"; string br2 = "0123456789012345678901234567890123456789012345678901234567890123456"; string str = "Now is the time for all good men to come to the aid of their party."; 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 33 to 66. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. The string 'he' occurs at position(s): 45 56 */
// Sample for String::IndexOf(String, Int32, Int32) using namespace System; int main() { String^ br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"; String^ br2 = "0123456789012345678901234567890123456789012345678901234567890123456"; String^ str = "Now is the time for all good men to come to the aid of their party."; 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 33 to 66. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. The string 'he' occurs at position(s): 45 56 */
// Sample for String.IndexOf(String, Int32, Int32) import System.*; class Sample { public static void main(String[] args) { String br1 = "0----+----1----+----2----+----3----+----4----+----5----" + "+----6----+-"; String br2 = "01234567890123456789012345678901234567890123456789012345" + "67890123456"; String str = "Now is the time for all good men to come to the aid of " + "their party."; int start; int at; int end; int count; end = str.get_Length(); start = end / 2; Console.WriteLine(); Console.WriteLine("All occurrences of 'he' from position {0} to {1}." , (Int32)start, (Int32)(end - 1)); Console.Write("{1}{0}", Environment.get_NewLine(), br1); Console.Write("{1}{0}", Environment.get_NewLine(), br2); Console.WriteLine("{1}{0}", Environment.get_NewLine(), 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} ", (Int32)at); start = at + 1; } Console.WriteLine(); } //main } //Sample /* This example produces the following results: All occurrences of 'he' from position 33 to 66. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. The string 'he' occurs at position(s): 45 56 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.IndexOf メソッド (String)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As String Dim value As String Dim returnValue As Integer returnValue = instance.IndexOf(value)
戻り値
その文字列が見つかった場合は、value のインデックス位置。見つからなかった場合は -1。value が Empty の場合、戻り値は 0 です。



IndexOf メソッドについては、次のコード例を参照してください。
Imports System Public Class InsertTest Public Shared Sub Main() Dim animal1 As String = "fox" Dim animal2 As String = "dog" Dim strTarget As String = [String].Format("The {0} jumped over the {1}.", animal1, animal2) Console.WriteLine("The original string is:{0}{1}{0}", Environment.NewLine, strTarget) Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal1) Dim adj1 As String = Console.ReadLine() Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal2) Dim adj2 As String = Console.ReadLine() adj1 = adj1.Trim() + " " adj2 = adj2.Trim() + " " strTarget = strTarget.Insert(strTarget.IndexOf(animal1), adj1) strTarget = strTarget.Insert(strTarget.IndexOf(animal2), adj2) Console.WriteLine("{0}The final string is:{0}{1}", Environment.NewLine, strTarget) End Sub 'Main End Class 'InsertTest
using System; public class InsertTest { public static void Main() { string animal1 = "fox"; string animal2 = "dog"; string strTarget = String.Format("The {0} jumped over the {1}.", animal1, animal2); Console.WriteLine("The original string is:{0}{1}{0}", Environment.NewLine, strTarget); Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal1); string adj1 = Console.ReadLine(); Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal2); string adj2 = Console.ReadLine(); adj1 = adj1.Trim() + " "; adj2 = adj2.Trim() + " "; strTarget = strTarget.Insert(strTarget.IndexOf(animal1), adj1); strTarget = strTarget.Insert(strTarget.IndexOf(animal2), adj2); Console.WriteLine("{0}The final string is:{0}{1}", Environment.NewLine, strTarget); } }
using namespace System; int main() { String^ animal1 = "fox"; String^ animal2 = "dog"; String^ strTarget = String::Format( "The {0} jumped over the {1}.", animal1, animal2 ); Console::WriteLine( "The original string is:{0}{1}{0}", Environment::NewLine, strTarget ); Console::Write( "Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal1 ); String^ adj1 = Console::ReadLine(); Console::Write( "Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal2 ); String^ adj2 = Console::ReadLine(); adj1 = String::Concat( adj1->Trim(), " " ); adj2 = String::Concat( adj2->Trim(), " " ); strTarget = strTarget->Insert( strTarget->IndexOf( animal1 ), adj1 ); strTarget = strTarget->Insert( strTarget->IndexOf( animal2 ), adj2 ); Console::WriteLine( " {0}The final string is: {0} {1}", Environment::NewLine, strTarget ); }
import System.*; public class InsertTest { public static void main(String[] args) { String animal1 = "fox"; String animal2 = "dog"; String strTarget = String.Format("The {0} jumped over the {1}.", animal1, animal2); Console.WriteLine("The original string is:{0}{1}{0}", Environment.get_NewLine(), strTarget); Console.Write("Please enter an adjective (or a group of adjectives) " + "to describe the {0}: ==> ", animal1); String adj1 = Console.ReadLine(); Console.Write("Please enter an adjective (or a group of adjectives) " + "to describe the {0}: ==> ", animal2); String adj2 = Console.ReadLine(); adj1 = adj1.Trim() + " "; adj2 = adj2.Trim() + " "; strTarget = strTarget.Insert(strTarget.IndexOf(animal1), adj1); strTarget = strTarget.Insert(strTarget.IndexOf(animal2), adj2); Console.WriteLine("{0}The final string is:{0}{1}", Environment.get_NewLine(), strTarget); } //main } //InsertTest
import System; public class InsertTest { public static function Main() : void { var animal1 : String = "fox"; var animal2 : String = "dog"; var strTarget : String = String.Format("The {0} jumped over the {1}.", animal1, animal2); Console.WriteLine("The original string is:{0}{1}{0}", Environment.NewLine, strTarget); Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal1); var adj1 : String = Console.ReadLine(); Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal2); var adj2 : String = Console.ReadLine(); adj1 = adj1.Trim() + " "; adj2 = adj2.Trim() + " "; strTarget = strTarget.Insert(strTarget.IndexOf(animal1), adj1); strTarget = strTarget.Insert(strTarget.IndexOf(animal2), adj2); Console.WriteLine("{0}The final string is:{0}{1}", Environment.NewLine, strTarget); } } InsertTest.Main();

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.IndexOf メソッド (Char)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As String Dim value As Char Dim returnValue As Integer returnValue = instance.IndexOf(value)
戻り値
その文字が見つかった場合は、value のインデックス位置。見つからなかった場合は -1。

このメソッドは、序数 (カルチャに依存しない) 検索を実行します。この検索方法では、2 つの文字は Unicode スカラ値が等しいときだけ等価と見なされます。カルチャに依存した検索を実行するには、CompareInfo.IndexOf メソッドを使用します。このメソッドを使用して検索すると、合字の "A" (U+00C6) のような構成済み文字を表す Unicode 値は、'AE' (U+0041, U+0045) のようにその文字の構成要素が正しい順序で出現した場合、これらの構成要素と (カルチャの種類に応じて) 等価と見なされます。

次に示すのは、IndexOf メソッドを使用して String の文字を検索するコード例です。
// Create a Unicode String with 5 Greek Alpha characters String szGreekAlpha = new String('\u0319',5); // Create a Unicode String with a Greek Omega character String szGreekOmega = new String(new char [] {'\u03A9','\u03A9','\u03A9'},2,1); String szGreekLetters = String.Concat(szGreekOmega, szGreekAlpha, szGreekOmega.Clone()); // Examine the result Console.WriteLine(szGreekLetters); // The first index of Alpha int ialpha = szGreekLetters.IndexOf('\u0319'); // The last index of Omega int iomega = szGreekLetters.LastIndexOf('\u03A9'); Console.WriteLine("The Greek letter Alpha first appears at index " + ialpha + " and Omega last appears at index " + iomega + " in this String.");
// Create a Unicode String with 5 Greek Alpha characters String^ szGreekAlpha = gcnew String( L'\x0319',5 ); // Create a Unicode String with a Greek Omega character wchar_t charArray5[3] = {L'\x03A9',L'\x03A9',L'\x03A9'}; String^ szGreekOmega = gcnew String( charArray5,2,1 ); String^ szGreekLetters = String::Concat( szGreekOmega, szGreekAlpha, szGreekOmega->Clone() ); // Examine the result Console::WriteLine( szGreekLetters ); // The first index of Alpha int ialpha = szGreekLetters->IndexOf( L'\x0319' ); // The last index of Omega int iomega = szGreekLetters->LastIndexOf( L'\x03A9' ); Console::WriteLine( String::Concat( "The Greek letter Alpha first appears at index ", Convert::ToString( ialpha ) ) ); Console::WriteLine( String::Concat( " and Omega last appears at index ", Convert::ToString( iomega ), " in this String." ) );

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.IndexOf メソッド (Char, Int32, Int32)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Function IndexOf ( _ value As Char, _ startIndex As Integer, _ count As Integer _ ) As Integer
Dim instance As String Dim value As Char Dim startIndex As Integer Dim count As Integer Dim returnValue As Integer returnValue = instance.IndexOf(value, startIndex, count)
戻り値
その文字が見つかった場合は、value のインデックス位置。見つからなかった場合は -1。


startIndex から startIndex + count -1 番目の位置まで検索が実行されましたが、startIndex + count の文字は検出されませんでした。
このメソッドは、序数 (カルチャに依存しない) 検索を実行します。この検索方法では、2 つの文字は Unicode スカラ値が等しいときだけ等価と見なされます。カルチャに依存した検索を実行するには、CompareInfo.IndexOf メソッドを使用します。このメソッドを使用して検索すると、合字の "A" (U+00C6) のような構成済み文字を表す Unicode 値は、'AE' (U+0041, U+0045) のようにその文字の構成要素が正しい順序で出現した場合、これらの構成要素と (カルチャの種類に応じて) 等価と見なされます。

' 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
// 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 */
// Example for the String::IndexOf( Char, int, int ) method. using namespace System; 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 ); } int 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 ); } /* 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 */
// Example for the String.IndexOf( char, int, int ) method. import System.*; class IndexOfCII { public static void main(String[] args) { 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}", new Object[] { Environment.get_NewLine(), br1, br2, str }); FindAllChar('A', str); FindAllChar('a', str); FindAllChar('I', str); FindAllChar('i', str); FindAllChar('@', str); FindAllChar(' ', str); } //main static void FindAllChar(char target, String searched) { Console.Write("The character '{0}' occurs at position(s): ", String.valueOf(target)); int startIndex = -1; int hitCount = 0; // Search for all occurrences of the target. while(true) { startIndex = searched.IndexOf(target, startIndex + 1, searched.get_Length() - startIndex - 1); // Exit the loop if the target is not found. if (startIndex < 0) { break; } Console.Write("{0}, ", String.valueOf(startIndex)); hitCount++; } Console.WriteLine("occurrences: {0}", String.valueOf(hitCount)); } //FindAllChar } //IndexOfCII /* 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 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.IndexOf メソッド (String, Int32, Int32, StringComparison)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Function IndexOf ( _ value As String, _ startIndex As Integer, _ count As Integer, _ comparisonType As StringComparison _ ) As Integer
Dim instance As String Dim value As String Dim startIndex As Integer Dim count As Integer Dim comparisonType As StringComparison Dim returnValue As Integer returnValue = instance.IndexOf(value, startIndex, count, comparisonType)
public function IndexOf ( value : String, startIndex : int, count : int, comparisonType : StringComparison ) : int
- comparisonType
System.StringComparison 値の 1 つ。
その文字列が見つかった場合は、value パラメータのインデックス位置。見つからなかった場合は -1。value が Empty の場合、戻り値は 0 です。


startIndex から startIndex + count -1 番目の位置まで検索が実行されましたが、startIndex + count の文字は検出されませんでした。
comparisonType パラメータは、現在のカルチャまたはインバリアント カルチャを使用し、大文字と小文字を区別する検索または区別しない検索を使用し、比較規則に単語または序数を使用して、value パラメータが検索されるように指定します。

StringComparison 列挙体の異なる値を使用して、ある文字列が別の文字列内で最初に出現する位置を検索する IndexOf メソッドの 3 つのオーバーロードのコード例を次に示します。
' This code example demonstrates the ' System.String.IndexOf(String, ..., StringComparison) methods. Imports System Imports System.Threading Imports System.Globalization Class Sample Public Shared Sub Main() Dim intro As String = "Find the first occurrence of a character using different " & _ "values of StringComparison." Dim resultFmt As String = "Comparison: {0,-28} Location: {1,3}" ' Define a string to search for. ' U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE Dim CapitalAWithRing As String = "Å" ' Define a string to search. ' The result of combining the characters LATIN SMALL LETTER A and COMBINING ' RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character ' LATIN SMALL LETTER A WITH RING ABOVE (U+00e5). Dim cat As String = "A Cheshire c" & "å" & "t" Dim loc As Integer = 0 Dim scValues As StringComparison() = { _ StringComparison.CurrentCulture, _ StringComparison.CurrentCultureIgnoreCase, _ StringComparison.InvariantCulture, _ StringComparison.InvariantCultureIgnoreCase, _ StringComparison.Ordinal, _ StringComparison.OrdinalIgnoreCase } Dim sc As StringComparison ' Clear the screen and display an introduction. Console.Clear() Console.WriteLine(intro) ' Display the current culture because culture affects the result. For example, ' try this code example with the "sv-SE" (Swedish-Sweden) culture. Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US") Console.WriteLine("The current culture is ""{0}"" - {1}.", _ Thread.CurrentThread.CurrentCulture.Name, _ Thread.CurrentThread.CurrentCulture.DisplayName) ' Display the string to search for and the string to search. Console.WriteLine("Search for the string ""{0}"" in the string ""{1}""", _ CapitalAWithRing, cat) Console.WriteLine() ' Note that in each of the following searches, we look for ' LATIN CAPITAL LETTER A WITH RING ABOVE in a string that contains ' LATIN SMALL LETTER A WITH RING ABOVE. A result value of -1 indicates ' the string was not found. ' Search using different values of StringComparsion. Specify the start ' index and count. Console.WriteLine("Part 1: Start index and count are specified.") For Each sc In scValues loc = cat.IndexOf(CapitalAWithRing, 0, cat.Length, sc) Console.WriteLine(resultFmt, sc, loc) Next sc ' Search using different values of StringComparsion. Specify the ' start index. Console.WriteLine(vbCrLf & "Part 2: Start index is specified.") For Each sc In scValues loc = cat.IndexOf(CapitalAWithRing, 0, sc) Console.WriteLine(resultFmt, sc, loc) Next sc ' Search using different values of StringComparsion. Console.WriteLine(vbCrLf & "Part 3: Neither start index nor count is specified.") For Each sc In scValues loc = cat.IndexOf(CapitalAWithRing, sc) Console.WriteLine(resultFmt, sc, loc) Next sc End Sub 'Main End Class 'Sample ' 'Note: This code example was executed on a console whose user interface 'culture is "en-US" (English-United States). ' 'This code example produces the following results: ' 'Find the first occurrence of a character using different values of StringComparison. 'The current culture is "en-US" - English (United States). 'Search for the string "Å" in the string "A Cheshire ca°t" ' 'Part 1: Start index and count are specified. 'Comparison: CurrentCulture Location: -1 'Comparison: CurrentCultureIgnoreCase Location: 12 'Comparison: InvariantCulture Location: -1 'Comparison: InvariantCultureIgnoreCase Location: 12 'Comparison: Ordinal Location: -1 'Comparison: OrdinalIgnoreCase Location: -1 ' 'Part 2: Start index is specified. 'Comparison: CurrentCulture Location: -1 'Comparison: CurrentCultureIgnoreCase Location: 12 'Comparison: InvariantCulture Location: -1 'Comparison: InvariantCultureIgnoreCase Location: 12 'Comparison: Ordinal Location: -1 'Comparison: OrdinalIgnoreCase Location: -1 ' 'Part 3: Neither start index nor count is specified. 'Comparison: CurrentCulture Location: -1 'Comparison: CurrentCultureIgnoreCase Location: 12 'Comparison: InvariantCulture Location: -1 'Comparison: InvariantCultureIgnoreCase Location: 12 'Comparison: Ordinal Location: -1 'Comparison: OrdinalIgnoreCase Location: -1 '
// This code example demonstrates the // System.String.IndexOf(String, ..., StringComparison) methods. using System; using System.Threading; using System.Globalization; class Sample { public static void Main() { string intro = "Find the first occurrence of a character using different " + "values of StringComparison."; string resultFmt = "Comparison: {0,-28} Location: {1 ,3}"; // Define a string to search for. // U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE string CapitalAWithRing = "\u00c5"; // Define a string to search. // The result of combining the characters LATIN SMALL LETTER A and COMBINING // RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character // LATIN SMALL LETTER A WITH RING ABOVE (U+00e5). string cat = "A Cheshire c" + "\u0061\u030a" + "t"; int loc = 0; StringComparison[] scValues = { StringComparison.CurrentCulture, StringComparison.CurrentCultureIgnoreCase, StringComparison.InvariantCulture, StringComparison.InvariantCultureIgnoreCase, StringComparison.Ordinal, StringComparison.OrdinalIgnoreCase }; // Clear the screen and display an introduction. Console.Clear(); Console.WriteLine(intro); // Display the current culture because culture affects the result. For example, // try this code example with the "sv-SE" (Swedish-Sweden) culture. Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); Console.WriteLine("The current culture is \"{0}\" - {1}.", Thread.CurrentThread.CurrentCulture.Name, Thread.CurrentThread.CurrentCulture.DisplayName); // Display the string to search for and the string to search. Console.WriteLine("Search for the string \"{0}\" in the string \"{1}\"", CapitalAWithRing, cat); Console.WriteLine(); // Note that in each of the following searches, we look for // LATIN CAPITAL LETTER A WITH RING ABOVE in a string that contains // LATIN SMALL LETTER A WITH RING ABOVE. A result value of -1 indicates // the string was not found. // Search using different values of StringComparsion. Specify the start // index and count. Console.WriteLine("Part 1: Start index and count are specified."); foreach (StringComparison sc in scValues) { loc = cat.IndexOf(CapitalAWithRing, 0, cat.Length, sc); Console.WriteLine(resultFmt, sc, loc); } // Search using different values of StringComparsion. Specify the // start index. Console.WriteLine("\nPart 2: Start index is specified."); foreach (StringComparison sc in scValues) { loc = cat.IndexOf(CapitalAWithRing, 0, sc); Console.WriteLine(resultFmt, sc, loc); } // Search using different values of StringComparsion. Console.WriteLine("\nPart 3: Neither start index nor count is specified."); foreach (StringComparison sc in scValues) { loc = cat.IndexOf(CapitalAWithRing, sc); Console.WriteLine(resultFmt, sc, loc); } } } /* Note: This code example was executed on a console whose user interface culture is "en-US" (English-United States). This code example produces the following results: Find the first occurrence of a character using different values of StringComparison. The current culture is "en-US" - English (United States). Search for the string "Å" in the string "A Cheshire ca°t" Part 1: Start index and count are specified. Comparison: CurrentCulture Location: -1 Comparison: CurrentCultureIgnoreCase Location: 12 Comparison: InvariantCulture Location: -1 Comparison: InvariantCultureIgnoreCase Location: 12 Comparison: Ordinal Location: -1 Comparison: OrdinalIgnoreCase Location: -1 Part 2: Start index is specified. Comparison: CurrentCulture Location: -1 Comparison: CurrentCultureIgnoreCase Location: 12 Comparison: InvariantCulture Location: -1 Comparison: InvariantCultureIgnoreCase Location: 12 Comparison: Ordinal Location: -1 Comparison: OrdinalIgnoreCase Location: -1 Part 3: Neither start index nor count is specified. Comparison: CurrentCulture Location: -1 Comparison: CurrentCultureIgnoreCase Location: 12 Comparison: InvariantCulture Location: -1 Comparison: InvariantCultureIgnoreCase Location: 12 Comparison: Ordinal Location: -1 Comparison: OrdinalIgnoreCase Location: -1 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.IndexOf メソッド (String, Int32, StringComparison)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Function IndexOf ( _ value As String, _ startIndex As Integer, _ comparisonType As StringComparison _ ) As Integer
Dim instance As String Dim value As String Dim startIndex As Integer Dim comparisonType As StringComparison Dim returnValue As Integer returnValue = instance.IndexOf(value, startIndex, comparisonType)
public function IndexOf ( value : String, startIndex : int, comparisonType : StringComparison ) : int
- comparisonType
System.StringComparison 値の 1 つ。
その文字列が見つかった場合は、value パラメータのインデックス位置。見つからなかった場合は -1。value が Empty の場合、戻り値は 0 です。


comparisonType パラメータは、現在のカルチャまたはインバリアント カルチャを使用し、大文字と小文字を区別する検索または区別しない検索を使用し、比較規則に単語または序数を使用して、value パラメータが検索されるように指定します。

StringComparison 列挙体の異なる値を使用して、ある文字列が別の文字列内で最初に出現する位置を検索する IndexOf メソッドの 3 つのオーバーロードのコード例を次に示します。
' This code example demonstrates the ' System.String.IndexOf(String, ..., StringComparison) methods. Imports System Imports System.Threading Imports System.Globalization Class Sample Public Shared Sub Main() Dim intro As String = "Find the first occurrence of a character using different " & _ "values of StringComparison." Dim resultFmt As String = "Comparison: {0,-28} Location: {1,3}" ' Define a string to search for. ' U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE Dim CapitalAWithRing As String = "Å" ' Define a string to search. ' The result of combining the characters LATIN SMALL LETTER A and COMBINING ' RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character ' LATIN SMALL LETTER A WITH RING ABOVE (U+00e5). Dim cat As String = "A Cheshire c" & "å" & "t" Dim loc As Integer = 0 Dim scValues As StringComparison() = { _ StringComparison.CurrentCulture, _ StringComparison.CurrentCultureIgnoreCase, _ StringComparison.InvariantCulture, _ StringComparison.InvariantCultureIgnoreCase, _ StringComparison.Ordinal, _ StringComparison.OrdinalIgnoreCase } Dim sc As StringComparison ' Clear the screen and display an introduction. Console.Clear() Console.WriteLine(intro) ' Display the current culture because culture affects the result. For example, ' try this code example with the "sv-SE" (Swedish-Sweden) culture. Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US") Console.WriteLine("The current culture is ""{0}"" - {1}.", _ Thread.CurrentThread.CurrentCulture.Name, _ Thread.CurrentThread.CurrentCulture.DisplayName) ' Display the string to search for and the string to search. Console.WriteLine("Search for the string ""{0}"" in the string ""{1}""", _ CapitalAWithRing, cat) Console.WriteLine() ' Note that in each of the following searches, we look for ' LATIN CAPITAL LETTER A WITH RING ABOVE in a string that contains ' LATIN SMALL LETTER A WITH RING ABOVE. A result value of -1 indicates ' the string was not found. ' Search using different values of StringComparsion. Specify the start ' index and count. Console.WriteLine("Part 1: Start index and count are specified.") For Each sc In scValues loc = cat.IndexOf(CapitalAWithRing, 0, cat.Length, sc) Console.WriteLine(resultFmt, sc, loc) Next sc ' Search using different values of StringComparsion. Specify the ' start index. Console.WriteLine(vbCrLf & "Part 2: Start index is specified.") For Each sc In scValues loc = cat.IndexOf(CapitalAWithRing, 0, sc) Console.WriteLine(resultFmt, sc, loc) Next sc ' Search using different values of StringComparsion. Console.WriteLine(vbCrLf & "Part 3: Neither start index nor count is specified.") For Each sc In scValues loc = cat.IndexOf(CapitalAWithRing, sc) Console.WriteLine(resultFmt, sc, loc) Next sc End Sub 'Main End Class 'Sample ' 'Note: This code example was executed on a console whose user interface 'culture is "en-US" (English-United States). ' 'This code example produces the following results: ' 'Find the first occurrence of a character using different values of StringComparison. 'The current culture is "en-US" - English (United States). 'Search for the string "Å" in the string "A Cheshire ca°t" ' 'Part 1: Start index and count are specified. 'Comparison: CurrentCulture Location: -1 'Comparison: CurrentCultureIgnoreCase Location: 12 'Comparison: InvariantCulture Location: -1 'Comparison: InvariantCultureIgnoreCase Location: 12 'Comparison: Ordinal Location: -1 'Comparison: OrdinalIgnoreCase Location: -1 ' 'Part 2: Start index is specified. 'Comparison: CurrentCulture Location: -1 'Comparison: CurrentCultureIgnoreCase Location: 12 'Comparison: InvariantCulture Location: -1 'Comparison: InvariantCultureIgnoreCase Location: 12 'Comparison: Ordinal Location: -1 'Comparison: OrdinalIgnoreCase Location: -1 ' 'Part 3: Neither start index nor count is specified. 'Comparison: CurrentCulture Location: -1 'Comparison: CurrentCultureIgnoreCase Location: 12 'Comparison: InvariantCulture Location: -1 'Comparison: InvariantCultureIgnoreCase Location: 12 'Comparison: Ordinal Location: -1 'Comparison: OrdinalIgnoreCase Location: -1 '
// This code example demonstrates the // System.String.IndexOf(String, ..., StringComparison) methods. using System; using System.Threading; using System.Globalization; class Sample { public static void Main() { string intro = "Find the first occurrence of a character using different " + "values of StringComparison."; string resultFmt = "Comparison: {0,-28} Location: {1 ,3}"; // Define a string to search for. // U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE string CapitalAWithRing = "\u00c5"; // Define a string to search. // The result of combining the characters LATIN SMALL LETTER A and COMBINING // RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character // LATIN SMALL LETTER A WITH RING ABOVE (U+00e5). string cat = "A Cheshire c" + "\u0061\u030a" + "t"; int loc = 0; StringComparison[] scValues = { StringComparison.CurrentCulture, StringComparison.CurrentCultureIgnoreCase, StringComparison.InvariantCulture, StringComparison.InvariantCultureIgnoreCase, StringComparison.Ordinal, StringComparison.OrdinalIgnoreCase }; // Clear the screen and display an introduction. Console.Clear(); Console.WriteLine(intro); // Display the current culture because culture affects the result. For example, // try this code example with the "sv-SE" (Swedish-Sweden) culture. Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); Console.WriteLine("The current culture is \"{0}\" - {1}.", Thread.CurrentThread.CurrentCulture.Name, Thread.CurrentThread.CurrentCulture.DisplayName); // Display the string to search for and the string to search. Console.WriteLine("Search for the string \"{0}\" in the string \"{1}\"", CapitalAWithRing, cat); Console.WriteLine(); // Note that in each of the following searches, we look for // LATIN CAPITAL LETTER A WITH RING ABOVE in a string that contains // LATIN SMALL LETTER A WITH RING ABOVE. A result value of -1 indicates // the string was not found. // Search using different values of StringComparsion. Specify the start // index and count. Console.WriteLine("Part 1: Start index and count are specified."); foreach (StringComparison sc in scValues) { loc = cat.IndexOf(CapitalAWithRing, 0, cat.Length, sc); Console.WriteLine(resultFmt, sc, loc); } // Search using different values of StringComparsion. Specify the // start index. Console.WriteLine("\nPart 2: Start index is specified."); foreach (StringComparison sc in scValues) { loc = cat.IndexOf(CapitalAWithRing, 0, sc); Console.WriteLine(resultFmt, sc, loc); } // Search using different values of StringComparsion. Console.WriteLine("\nPart 3: Neither start index nor count is specified."); foreach (StringComparison sc in scValues) { loc = cat.IndexOf(CapitalAWithRing, sc); Console.WriteLine(resultFmt, sc, loc); } } } /* Note: This code example was executed on a console whose user interface culture is "en-US" (English-United States). This code example produces the following results: Find the first occurrence of a character using different values of StringComparison. The current culture is "en-US" - English (United States). Search for the string "Å" in the string "A Cheshire ca°t" Part 1: Start index and count are specified. Comparison: CurrentCulture Location: -1 Comparison: CurrentCultureIgnoreCase Location: 12 Comparison: InvariantCulture Location: -1 Comparison: InvariantCultureIgnoreCase Location: 12 Comparison: Ordinal Location: -1 Comparison: OrdinalIgnoreCase Location: -1 Part 2: Start index is specified. Comparison: CurrentCulture Location: -1 Comparison: CurrentCultureIgnoreCase Location: 12 Comparison: InvariantCulture Location: -1 Comparison: InvariantCultureIgnoreCase Location: 12 Comparison: Ordinal Location: -1 Comparison: OrdinalIgnoreCase Location: -1 Part 3: Neither start index nor count is specified. Comparison: CurrentCulture Location: -1 Comparison: CurrentCultureIgnoreCase Location: 12 Comparison: InvariantCulture Location: -1 Comparison: InvariantCultureIgnoreCase Location: 12 Comparison: Ordinal Location: -1 Comparison: OrdinalIgnoreCase Location: -1 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.IndexOf メソッド (String, StringComparison)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As String Dim value As String Dim comparisonType As StringComparison Dim returnValue As Integer returnValue = instance.IndexOf(value, comparisonType)
- comparisonType
System.StringComparison 値の 1 つ。
その文字列が見つかった場合は、value パラメータのインデックス位置。見つからなかった場合は -1。value が Empty の場合、戻り値は 0 です。


comparisonType パラメータは、現在のカルチャまたはインバリアント カルチャを使用し、大文字と小文字を区別する検索または区別しない検索を使用し、比較規則に単語または序数を使用して、value パラメータが検索されるように指定します。

StringComparison 列挙体の異なる値を使用して、ある文字列が別の文字列内で最初に出現する位置を検索する IndexOf メソッドの 3 つのオーバーロードのコード例を次に示します。
' This code example demonstrates the ' System.String.IndexOf(String, ..., StringComparison) methods. Imports System Imports System.Threading Imports System.Globalization Class Sample Public Shared Sub Main() Dim intro As String = "Find the first occurrence of a character using different " & _ "values of StringComparison." Dim resultFmt As String = "Comparison: {0,-28} Location: {1,3}" ' Define a string to search for. ' U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE Dim CapitalAWithRing As String = "Å" ' Define a string to search. ' The result of combining the characters LATIN SMALL LETTER A and COMBINING ' RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character ' LATIN SMALL LETTER A WITH RING ABOVE (U+00e5). Dim cat As String = "A Cheshire c" & "å" & "t" Dim loc As Integer = 0 Dim scValues As StringComparison() = { _ StringComparison.CurrentCulture, _ StringComparison.CurrentCultureIgnoreCase, _ StringComparison.InvariantCulture, _ StringComparison.InvariantCultureIgnoreCase, _ StringComparison.Ordinal, _ StringComparison.OrdinalIgnoreCase } Dim sc As StringComparison ' Clear the screen and display an introduction. Console.Clear() Console.WriteLine(intro) ' Display the current culture because culture affects the result. For example, ' try this code example with the "sv-SE" (Swedish-Sweden) culture. Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US") Console.WriteLine("The current culture is ""{0}"" - {1}.", _ Thread.CurrentThread.CurrentCulture.Name, _ Thread.CurrentThread.CurrentCulture.DisplayName) ' Display the string to search for and the string to search. Console.WriteLine("Search for the string ""{0}"" in the string ""{1}""", _ CapitalAWithRing, cat) Console.WriteLine() ' Note that in each of the following searches, we look for ' LATIN CAPITAL LETTER A WITH RING ABOVE in a string that contains ' LATIN SMALL LETTER A WITH RING ABOVE. A result value of -1 indicates ' the string was not found. ' Search using different values of StringComparsion. Specify the start ' index and count. Console.WriteLine("Part 1: Start index and count are specified.") For Each sc In scValues loc = cat.IndexOf(CapitalAWithRing, 0, cat.Length, sc) Console.WriteLine(resultFmt, sc, loc) Next sc ' Search using different values of StringComparsion. Specify the ' start index. Console.WriteLine(vbCrLf & "Part 2: Start index is specified.") For Each sc In scValues loc = cat.IndexOf(CapitalAWithRing, 0, sc) Console.WriteLine(resultFmt, sc, loc) Next sc ' Search using different values of StringComparsion. Console.WriteLine(vbCrLf & "Part 3: Neither start index nor count is specified.") For Each sc In scValues loc = cat.IndexOf(CapitalAWithRing, sc) Console.WriteLine(resultFmt, sc, loc) Next sc End Sub 'Main End Class 'Sample ' 'Note: This code example was executed on a console whose user interface 'culture is "en-US" (English-United States). ' 'This code example produces the following results: ' 'Find the first occurrence of a character using different values of StringComparison. 'The current culture is "en-US" - English (United States). 'Search for the string "Å" in the string "A Cheshire ca°t" ' 'Part 1: Start index and count are specified. 'Comparison: CurrentCulture Location: -1 'Comparison: CurrentCultureIgnoreCase Location: 12 'Comparison: InvariantCulture Location: -1 'Comparison: InvariantCultureIgnoreCase Location: 12 'Comparison: Ordinal Location: -1 'Comparison: OrdinalIgnoreCase Location: -1 ' 'Part 2: Start index is specified. 'Comparison: CurrentCulture Location: -1 'Comparison: CurrentCultureIgnoreCase Location: 12 'Comparison: InvariantCulture Location: -1 'Comparison: InvariantCultureIgnoreCase Location: 12 'Comparison: Ordinal Location: -1 'Comparison: OrdinalIgnoreCase Location: -1 ' 'Part 3: Neither start index nor count is specified. 'Comparison: CurrentCulture Location: -1 'Comparison: CurrentCultureIgnoreCase Location: 12 'Comparison: InvariantCulture Location: -1 'Comparison: InvariantCultureIgnoreCase Location: 12 'Comparison: Ordinal Location: -1 'Comparison: OrdinalIgnoreCase Location: -1 '
// This code example demonstrates the // System.String.IndexOf(String, ..., StringComparison) methods. using System; using System.Threading; using System.Globalization; class Sample { public static void Main() { string intro = "Find the first occurrence of a character using different " + "values of StringComparison."; string resultFmt = "Comparison: {0,-28} Location: {1 ,3}"; // Define a string to search for. // U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE string CapitalAWithRing = "\u00c5"; // Define a string to search. // The result of combining the characters LATIN SMALL LETTER A and COMBINING // RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character // LATIN SMALL LETTER A WITH RING ABOVE (U+00e5). string cat = "A Cheshire c" + "\u0061\u030a" + "t"; int loc = 0; StringComparison[] scValues = { StringComparison.CurrentCulture, StringComparison.CurrentCultureIgnoreCase, StringComparison.InvariantCulture, StringComparison.InvariantCultureIgnoreCase, StringComparison.Ordinal, StringComparison.OrdinalIgnoreCase }; // Clear the screen and display an introduction. Console.Clear(); Console.WriteLine(intro); // Display the current culture because culture affects the result. For example, // try this code example with the "sv-SE" (Swedish-Sweden) culture. Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); Console.WriteLine("The current culture is \"{0}\" - {1}.", Thread.CurrentThread.CurrentCulture.Name, Thread.CurrentThread.CurrentCulture.DisplayName); // Display the string to search for and the string to search. Console.WriteLine("Search for the string \"{0}\" in the string \"{1}\"", CapitalAWithRing, cat); Console.WriteLine(); // Note that in each of the following searches, we look for // LATIN CAPITAL LETTER A WITH RING ABOVE in a string that contains // LATIN SMALL LETTER A WITH RING ABOVE. A result value of -1 indicates // the string was not found. // Search using different values of StringComparsion. Specify the start // index and count. Console.WriteLine("Part 1: Start index and count are specified."); foreach (StringComparison sc in scValues) { loc = cat.IndexOf(CapitalAWithRing, 0, cat.Length, sc); Console.WriteLine(resultFmt, sc, loc); } // Search using different values of StringComparsion. Specify the // start index. Console.WriteLine("\nPart 2: Start index is specified."); foreach (StringComparison sc in scValues) { loc = cat.IndexOf(CapitalAWithRing, 0, sc); Console.WriteLine(resultFmt, sc, loc); } // Search using different values of StringComparsion. Console.WriteLine("\nPart 3: Neither start index nor count is specified."); foreach (StringComparison sc in scValues) { loc = cat.IndexOf(CapitalAWithRing, sc); Console.WriteLine(resultFmt, sc, loc); } } } /* Note: This code example was executed on a console whose user interface culture is "en-US" (English-United States). This code example produces the following results: Find the first occurrence of a character using different values of StringComparison. The current culture is "en-US" - English (United States). Search for the string "Å" in the string "A Cheshire ca°t" Part 1: Start index and count are specified. Comparison: CurrentCulture Location: -1 Comparison: CurrentCultureIgnoreCase Location: 12 Comparison: InvariantCulture Location: -1 Comparison: InvariantCultureIgnoreCase Location: 12 Comparison: Ordinal Location: -1 Comparison: OrdinalIgnoreCase Location: -1 Part 2: Start index is specified. Comparison: CurrentCulture Location: -1 Comparison: CurrentCultureIgnoreCase Location: 12 Comparison: InvariantCulture Location: -1 Comparison: InvariantCultureIgnoreCase Location: 12 Comparison: Ordinal Location: -1 Comparison: OrdinalIgnoreCase Location: -1 Part 3: Neither start index nor count is specified. Comparison: CurrentCulture Location: -1 Comparison: CurrentCultureIgnoreCase Location: 12 Comparison: InvariantCulture Location: -1 Comparison: InvariantCultureIgnoreCase Location: 12 Comparison: Ordinal Location: -1 Comparison: OrdinalIgnoreCase Location: -1 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からString.IndexOfを検索する場合は、下記のリンクをクリックしてください。

- String.IndexOfのページへのリンク