C# String IndexOf(String, StringComparison)

Description

String IndexOf(String, StringComparison) reports the zero-based index of the first occurrence of the specified string in the current String object. A parameter specifies the type of search to use for the specified string.

Syntax

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


public int IndexOf(
  string value,
  StringComparison comparisonType
)

Parameters

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

  • value - The string to seek.
  • comparisonType - One of the enumeration values that specifies the rules for the search.

Returns

String.IndexOf(String, StringComparison) method returns The 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 0.

Example

In the following example, the IndexOf(String, StringComparison) method is used.


/*  w  w w. ja v  a  2  s .  co  m*/

using System;

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

      // Use culture-sensitive comparison to find the soft hyphen.
      Console.WriteLine(s1.IndexOf("\u00AD", StringComparison.CurrentCulture));
      Console.WriteLine(s2.IndexOf("\u00AD", StringComparison.CurrentCulture));

   }
}

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