C# String IndexOf(Char, Int32)

Description

String IndexOf(Char, Int32) reports the zero-based index of the first occurrence of the specified Unicode character in this string. The search starts at a specified character position.

Syntax

String.IndexOf(Char, Int32) has the following syntax.


public int IndexOf(
  char value,
  int startIndex
)

Parameters

String.IndexOf(Char, Int32) has the following parameters.

  • value - A Unicode character to seek.
  • startIndex - The search starting position.

Returns

String.IndexOf(Char, Int32) method returns The zero-based index position of value if that character is found, or -1 if it is not.

Example

The following example demonstrates the IndexOf method.


//from  www. j  av  a 2  s.co m
using System;

class Sample {
    public static void Main() {

        string str = "This is a test from java2s.com.";
        int start = 0; 
        int at = 0;
        while((start < str.Length) && (at > -1)){
            at = str.IndexOf('t', start);
            if (at == -1) break;
            Console.Write("{0} ", at);
            start = at+1;
        }

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version