EN
Ce contenu n’est pas disponible dans votre langue. Voici la version anglaise.
18 sur 56 ont trouvé cela utile - Évaluez ce sujet

String.IndexOf Method

Reports the index of the first occurrence of a String, or one or more characters, within this instance.

Overload List

Reports the index of the first occurrence of the specified Unicode character in this instance.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function IndexOf(Char) As Integer
[C#] public int IndexOf(char);
[C++] public: int IndexOf(__wchar_t);
[JScript] public function IndexOf(Char) : int;

Reports the index of the first occurrence of the specified String in this instance.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function IndexOf(String) As Integer
[C#] public int IndexOf(string);
[C++] public: int IndexOf(String*);
[JScript] public function IndexOf(String) : int;

Reports the index of the first occurrence of the specified Unicode character in this instance. The search starts at a specified character position.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function IndexOf(Char, Integer) As Integer
[C#] public int IndexOf(char, int);
[C++] public: int IndexOf(__wchar_t, int);
[JScript] public function IndexOf(Char, int) : int;

Reports the index of the first occurrence of the specified String in this instance. The search starts at a specified character position.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function IndexOf(String, Integer) As Integer
[C#] public int IndexOf(string, int);
[C++] public: int IndexOf(String*, int);
[JScript] public function IndexOf(String, int) : int;

Reports the index of the first occurrence of the specified character in this instance. The search starts at a specified character position and examines a specified number of character positions.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function IndexOf(Char, Integer, Integer) As Integer
[C#] public int IndexOf(char, int, int);
[C++] public: int IndexOf(__wchar_t, int, int);
[JScript] public function IndexOf(Char, int, int) : int;

Reports the index of the first occurrence of the specified String in this instance. The search starts at a specified character position and examines a specified number of character positions.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function IndexOf(String, Integer, Integer) As Integer
[C#] public int IndexOf(string, int, int);
[C++] public: int IndexOf(String*, int, int);
[JScript] public function IndexOf(String, int, int) : int;

Example

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of IndexOf. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' 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
'
'

[C#] 
// 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

*/

[C++] 
// Sample for String::IndexOf(String, Int32, Int32)
#using <mscorlib.dll>

using namespace System;

int main() {

   String* br1 = S"0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
   String* br2 = S"0123456789012345678901234567890123456789012345678901234567890123456";
   String* str = S"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(S"All occurrences of 'he' from position {0} to {1}.", __box( start), __box( end-1));
   Console::WriteLine(S"{1}{0}{2}{0}{3}{0}", Environment::NewLine, br1, br2, str);
   Console::Write(S"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(S"he", start, count);
      if (at == -1) break;
      Console::Write(S"{0} ", __box( 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

*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

String Class | String Members | System Namespace

Cela vous a-t-il été utile ?
(1500 caractères restants)
Merci pour vos suggestions.
Afficher:
© 2014 Microsoft. Tous droits réservés.