C# String IndexOf(String, Int32, Int32, StringComparison)

Description

String IndexOf(String, Int32, Int32, StringComparison) reports the zero-based index of the first occurrence of the specified string in the current String object. Parameters specify the starting search position in the current string, the number of characters in the current string to search, and the type of search to use for the specified string.

Syntax

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


public int IndexOf(
  string value,//  www.  j a v a 2 s.c  o  m
  int startIndex,
  int count,
  StringComparison comparisonType
)

Parameters

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

  • value - The string to seek.
  • startIndex - The search starting position.
  • count - The number of character positions to examine.
  • comparisonType - One of the enumeration values that specifies the rules for the search.

Returns

String.IndexOf(String, Int32, Int32, StringComparison) method returns The zero-based index position of the value parameter if that string is found, or -1 if it is not. If value is Empty, the return value is startIndex.

Example

In the following example, the IndexOf(String, Int32, Int32, StringComparison) method is used to find the position of a soft hyphen (U+00AD) followed by an "m" starting in the third through sixth character positions in two strings.


//w  ww  .  jav a  2s. co m
using System;

public class Example
{
   public static void Main()
   {
      string searchString = "\u00ADm";
      string s1 = "ani\u00ADmal" ;
      string s2 = "animal";

      Console.WriteLine(s1.IndexOf(searchString, 2, 4, StringComparison.CurrentCulture));
      Console.WriteLine(s1.IndexOf(searchString, 2, 4, StringComparison.Ordinal));
      Console.WriteLine(s2.IndexOf(searchString, 2, 4, StringComparison.CurrentCulture));
      Console.WriteLine(s2.IndexOf(searchString, 2, 4, StringComparison.Ordinal));
   }
}

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