String.IndexOf メソッドとは? わかりやすく解説

String.IndexOf メソッド (Char, Int32)

指定した Unicode 文字がこの文字列内で最初に見つかった位置インデックスレポートします検索は、指定した文字位置から開始されます。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Function IndexOf ( _
    value As Char, _
    startIndex As Integer _
) As Integer
Dim instance As String
Dim value As Char
Dim startIndex As Integer
Dim returnValue As Integer

returnValue = instance.IndexOf(value, startIndex)
public int IndexOf (
    char value,
    int startIndex
)
public:
int IndexOf (
    wchar_t value, 
    int startIndex
)
public int IndexOf (
    char value, 
    int startIndex
)
public function IndexOf (
    value : char, 
    startIndex : int
) : int

パラメータ

value

シークする Unicode 文字

startIndex

検索開始される位置

戻り値
その文字見つかった場合は、valueインデックス位置見つからなかった場合は -1。

例外例外
例外種類条件

ArgumentOutOfRangeException

startIndex が 0 未満か、インスタンス末尾越えた位置指定してます。

解説解説

インデックス番号付けは 0 から始まります

検索範囲startIndex から文字列末尾までです。

value検索では大文字と小文字区別されます。

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

使用例使用例

IndexOf メソッドコード例次に示します

' 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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.IndexOf メソッド

String (1 つ上の文字) がこの文字列内で最初に見つかった位置インデックスレポートします
オーバーロードの一覧オーバーロードの一覧

名前 説明
String.IndexOf (Char) 指定した Unicode 文字がこの文字列内で最初に見つかった位置インデックスレポートします

.NET Compact Framework によってサポートされています。

String.IndexOf (String) 指定した String がこのインスタンス内で最初に見つかった位置インデックスレポートします

.NET Compact Framework によってサポートされています。

String.IndexOf (Char, Int32) 指定した Unicode 文字がこの文字列内で最初に見つかった位置インデックスレポートします検索は、指定した文字位置から開始されます。

.NET Compact Framework によってサポートされています。

String.IndexOf (String, Int32) 指定した String がこのインスタンス内で最初に見つかった位置インデックスレポートします検索は、指定した文字位置から開始されます。

.NET Compact Framework によってサポートされています。

String.IndexOf (String, StringComparison) 指定した文字列現在の String オブジェクト内で最初に見つかった位置インデックスレポートします指定した文字列使用する検索種類指定するパラメータ

.NET Compact Framework によってサポートされています。

String.IndexOf (Char, Int32, Int32) 指定文字がこのインスタンス内で最初に見つかった位置インデックスレポートします検索指定した文字位置から開始され指定した数の文字位置検査されます。

.NET Compact Framework によってサポートされています。

String.IndexOf (String, Int32, Int32) 指定した String がこのインスタンス内で最初に見つかった位置インデックスレポートします検索指定した文字位置から開始され指定した数の文字位置検査されます。

.NET Compact Framework によってサポートされています。

String.IndexOf (String, Int32, StringComparison) 指定した文字列現在の String オブジェクト内で最初に見つかった位置インデックスレポートしますパラメータは、現在の文字列内での検索開始位置指定し指定した文字列使用する検索種類指定します

.NET Compact Framework によってサポートされています。

String.IndexOf (String, Int32, Int32, StringComparison) 指定した文字列現在の String オブジェクト内で最初に見つかった位置インデックスレポートしますパラメータは、現在の文字列での検索位置現在の文字列で検索する文字の数、および指定した文字列使用する検索種類指定します

.NET Compact Framework によってサポートされています。

参照参照

関連項目

String クラス
String メンバ
System 名前空間
Char 構造体
IndexOfAny
LastIndexOf
LastIndexOfAny

String.IndexOf メソッド (String, Int32)

指定した String がこのインスタンス内で最初に見つかった位置インデックスレポートします検索は、指定した文字位置から開始されます。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Function IndexOf ( _
    value As String, _
    startIndex As Integer _
) As Integer
Dim instance As String
Dim value As String
Dim startIndex As Integer
Dim returnValue As Integer

returnValue = instance.IndexOf(value, startIndex)
public int IndexOf (
    string value,
    int startIndex
)
public:
int IndexOf (
    String^ value, 
    int startIndex
)
public int IndexOf (
    String value, 
    int startIndex
)
public function IndexOf (
    value : String, 
    startIndex : int
) : int

パラメータ

value

シークする String

startIndex

検索開始される位置

戻り値
その文字列見つかった場合は、valueインデックス位置見つからなかった場合は -1。valueEmpty場合戻り値startIndex です。

例外例外
例外種類条件

ArgumentNullException

valuenull 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

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();
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
String クラス
String メンバ
System 名前空間
Int32 構造体
CultureInfo
IndexOfAny
LastIndexOf
LastIndexOfAny

String.IndexOf メソッド (String, Int32, Int32)

指定した String がこのインスタンス内で最初に見つかった位置インデックスレポートします検索指定した文字位置から開始され指定した数の文字位置検査されます。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Function IndexOf ( _
    value As String, _
    startIndex As Integer, _
    count As Integer _
) As Integer
public int IndexOf (
    string value,
    int startIndex,
    int count
)
public:
int IndexOf (
    String^ value, 
    int startIndex, 
    int count
)
public int IndexOf (
    String value, 
    int startIndex, 
    int count
)
public function IndexOf (
    value : String, 
    startIndex : int, 
    count : int
) : int

パラメータ

value

シークする String

startIndex

検索開始される位置

count

検査する文字位置の数。

戻り値
その文字列見つかった場合は、valueインデックス位置見つからなかった場合は -1。valueEmpty場合戻り値startIndex です。

例外例外
例外種類条件

ArgumentNullException

valuenull 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

count または startIndex が負の値です。

または

startIndexcount足した数が、このインスタンス内にない位置示してます。

解説解説

インデックス番号付けは 0 から始まります

このメソッドは、現在のカルチャを使用して単語 (大文字/小文字区別し、カルチャに依存した) 検索実行します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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
String クラス
String メンバ
System 名前空間
Int32 構造体
CultureInfo
IndexOfAny
LastIndexOf
LastIndexOfAny

String.IndexOf メソッド (String)

指定した String がこのインスタンス内で最初に見つかった位置インデックスレポートします

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

ArgumentNullException

valuenull 参照 (Visual Basic では Nothing) です。

解説解説
使用例使用例

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();
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
String クラス
String メンバ
System 名前空間
Int32 構造体
CultureInfo
IndexOfAny
LastIndexOf
LastIndexOfAny

String.IndexOf メソッド (Char)

指定した Unicode 文字がこの文字列内で最初に見つかった位置インデックスレポートします

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

解説解説

インデックス番号付けは 0 から始まります

value検索では大文字と小文字区別されます。

このメソッドは、序数 (カルチャに依存しない) 検索実行します。この検索方法では、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." ) );
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.IndexOf メソッド (Char, Int32, Int32)

指定文字がこのインスタンス内で最初に見つかった位置インデックスレポートします検索指定した文字位置から開始され指定した数の文字位置検査されます。

名前空間: System
アセンブリ: 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)
public int IndexOf (
    char value,
    int startIndex,
    int count
)
public:
int IndexOf (
    wchar_t value, 
    int startIndex, 
    int count
)
public int IndexOf (
    char value, 
    int startIndex, 
    int count
)
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) のようにその文字構成要素正し順序出現した場合、これらの構成要素と (カルチャの種類に応じて) 等価と見なされます

使用例使用例

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
// 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
*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.IndexOf メソッド (String, Int32, Int32, StringComparison)

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

指定した文字列現在の String オブジェクト内で最初に見つかった位置インデックスレポートしますパラメータは、現在の文字列での検索位置現在の文字列で検索する文字の数、および指定した文字列使用する検索種類指定します

名前空間: System
アセンブリ: 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 int IndexOf (
    string value,
    int startIndex,
    int count,
    StringComparison comparisonType
)
public:
int IndexOf (
    String^ value, 
    int startIndex, 
    int count, 
    StringComparison comparisonType
)
public int IndexOf (
    String value, 
    int startIndex, 
    int count, 
    StringComparison comparisonType
)
public function IndexOf (
    value : String, 
    startIndex : int, 
    count : int, 
    comparisonType : StringComparison
) : int

パラメータ

value

シークする String オブジェクト

startIndex

検索開始される位置

count

検査する文字位置の数。

comparisonType

System.StringComparison 値の 1 つ

戻り値
その文字列見つかった場合は、value パラメータインデックス位置見つからなかった場合は -1。valueEmpty場合戻り値は 0 です。

例外例外
例外種類条件

ArgumentNullException

valuenull 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

count または startIndex が負の値です。

または

startIndexcount足した数が、このインスタンス内にない位置示してます。

ArgumentException

comparisonType有効な System.StringComparison 値ではありません。

解説解説

インデックス番号付けは 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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.IndexOf メソッド (String, Int32, StringComparison)

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

指定した文字列現在の String オブジェクト内で最初に見つかった位置インデックスレポートしますパラメータは、現在の文字列内での検索開始位置指定し指定した文字列使用する検索種類指定します

名前空間: System
アセンブリ: 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 int IndexOf (
    string value,
    int startIndex,
    StringComparison comparisonType
)
public:
int IndexOf (
    String^ value, 
    int startIndex, 
    StringComparison comparisonType
)
public int IndexOf (
    String value, 
    int startIndex, 
    StringComparison comparisonType
)
public function IndexOf (
    value : String, 
    startIndex : int, 
    comparisonType : StringComparison
) : int

パラメータ

value

シークする String オブジェクト

startIndex

検索開始される位置

comparisonType

System.StringComparison 値の 1 つ

戻り値
その文字列見つかった場合は、value パラメータインデックス位置見つからなかった場合は -1。valueEmpty場合戻り値は 0 です。

例外例外
例外種類条件

ArgumentNullException

valuenull 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

startIndex負の数であるか、またはこのインスタンス範囲外位置指定してます。

ArgumentException

comparisonType有効な System.StringComparison 値ではありません。

解説解説

インデックス番号付けは 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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.IndexOf メソッド (String, StringComparison)

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

指定した文字列現在の String オブジェクト内で最初に見つかった位置インデックスレポートします指定した文字列使用する検索種類指定するパラメータ

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Function IndexOf ( _
    value As String, _
    comparisonType As StringComparison _
) As Integer
Dim instance As String
Dim value As String
Dim comparisonType As StringComparison
Dim returnValue As Integer

returnValue = instance.IndexOf(value, comparisonType)
public int IndexOf (
    string value,
    StringComparison comparisonType
)
public:
int IndexOf (
    String^ value, 
    StringComparison comparisonType
)
public int IndexOf (
    String value, 
    StringComparison comparisonType
)
public function IndexOf (
    value : String, 
    comparisonType : StringComparison
) : int

パラメータ

value

シークする String オブジェクト

comparisonType

System.StringComparison 値の 1 つ

戻り値
その文字列見つかった場合は、value パラメータインデックス位置見つからなかった場合は -1。valueEmpty場合戻り値は 0 です。

例外例外
例外種類条件

ArgumentNullException

valuenull 参照 (Visual Basic では Nothing) です。

ArgumentException

comparisonType有効な System.StringComparison 値ではありません。

解説解説

インデックス番号付けは 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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からString.IndexOf メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からString.IndexOf メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からString.IndexOf メソッドを検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

String.IndexOf メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



String.IndexOf メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS