C# String IndexOf(Char)

Description

String IndexOf(Char) reports the zero-based index of the first occurrence of the specified Unicode character in this string.

Syntax

String.IndexOf(Char) has the following syntax.


public int IndexOf(
  char value
)

Parameters

String.IndexOf(Char) has the following parameters.

  • value - A Unicode character to seek.

Returns

String.IndexOf(Char) 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 how you can search a String for a character using the IndexOf method.


//from   www  . j a v a 2  s. c  om
using System;
public class MainClass{
  public static void Main(String[] argv){  
    String szGreekAlpha = new String('\u0391',5);
    String szGreekOmega = new String(new char [] {'\u03A9','\u03A9','\u03A9'},2,1);
    
    String szGreekLetters = String.Concat(szGreekOmega, szGreekAlpha, szGreekOmega.Clone());
    
    int ialpha = szGreekLetters.IndexOf('\u0391');
    
    int iomega = szGreekLetters.LastIndexOf('\u03A9');
    
    Console.WriteLine(ialpha);
    Console.WriteLine(iomega);
  }
}
    

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