System.String.IndexOf 方法 (Char, Int32, Int32)

方法描述

报告指定字符在此实例中的第一个匹配项的索引。 搜索从指定字符位置开始,并检查指定数量的字符位置。

语法定义(C# System.String.IndexOf 方法 (Char, Int32, Int32) 的用法)

public int IndexOf(
	char value,
	int startIndex,
	int count
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
value System-Char 要查找的 Unicode 字符。
startIndex System-Int32 搜索起始位置。
count System-Int32 要检查的字符位置数。
返回值 System.Int32 如果找到该字符,则为 value 的从零开始的索引位置;如果未找到,则为 -1。

提示和注释

搜索从 startIndex 开始,并且一直搜索至 startIndex + count -1。 startIndex + count 位置上的字符不在搜索范围内。

索引编号从零开始。 startIndex 参数的范围可以从 0 到字符串实例的长度减一。

此方法执行顺序(不区分区域性)搜索,即仅当 Unicode 标量值相同时两个字符才被视为相等。 若要执行区分区域性的搜索,请使用 CompareInfo.IndexOf 方法,根据所使用的区域性,该方法可能将表示预先构成字符(如连字“Æ”(U+00C6))的 Unicode 标量值视为等同于任何顺序正确的该字符的组成字符,如上述连字可能被视为“AE”(U+0041、U+0045)。

System.String.IndexOf 方法 (Char, Int32, Int32)例子

下面的示例说明 IndexOf 方法。

// Example for the String.IndexOf( char, int, int ) method.
using System;

class IndexOfCII 
{
    public static void Main() 
    {
        string br1 = 
            "0----+----1----+----2----+----3----+----" +
            "4----+----5----+----6----+----7";
        string br2 = 
            "0123456789012345678901234567890123456789" +
            "0123456789012345678901234567890";
        string str = 
            "ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi " +
            "ABCDEFGHI abcdefghi ABCDEFGHI";

        Console.WriteLine( 
            "This example of String.IndexOf( char, int, int )\n" +
            "generates the following output." );
        Console.WriteLine( 
            "{0}{1}{0}{2}{0}{3}{0}", 
            Environment.NewLine, br1, br2, str );

        FindAllChar( 'A', str );
        FindAllChar( 'a', str );
        FindAllChar( 'I', str );
        FindAllChar( 'i', str );
        FindAllChar( '@', str );
        FindAllChar( ' ', str );
    }

    static void FindAllChar( Char target, String searched )
    {
        Console.Write( 
            "The character '{0}' occurs at position(s): ", 
            target );

        int     startIndex = -1;
        int     hitCount = 0;

        // Search for all occurrences of the target.
        while( true )
        {
            startIndex = searched.IndexOf( 
                target, startIndex + 1, 
                searched.Length - startIndex - 1 );

            // Exit the loop if the target is not found.
            if( startIndex < 0 )
                break;

            Console.Write( "{0}, ", startIndex );
            hitCount++;
        }

        Console.WriteLine( "occurrences: {0}", hitCount );
    }
}

/*
This example of String.IndexOf( char, int, int )
generates the following output.

0----+----1----+----2----+----3----+----4----+----5----+----6----+----7
01234567890123456789012345678901234567890123456789012345678901234567890
ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi ABCDEFGHI

The character 'A' occurs at position(s): 0, 20, 40, 60, occurrences: 4
The character 'a' occurs at position(s): 10, 30, 50, occurrences: 3
The character 'I' occurs at position(s): 8, 28, 48, 68, occurrences: 4
The character 'i' occurs at position(s): 18, 38, 58, occurrences: 3
The character '@' occurs at position(s): occurrences: 0
The character ' ' occurs at position(s): 9, 19, 29, 39, 49, 59, occurrences: 6
*/

异常

异常 异常描述
ArgumentOutOfRangeException
  • count 或 startIndex 为负。
  • count+startIndex 指定一个超出此实例末尾的位置。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。