C#|字符串。IndexOf()方法| Set–1

在C#中, IndexOf() 方法是一种 一串 方法此方法用于查找指定字符或字符串在该字符串的当前实例中第一次出现的从零开始的索引。如果找不到字符或字符串,该方法将返回-1。通过向该方法传递不同的参数,可以重载该方法。

null
  • 一串IndexOf(char x)
  • 一串IndexOf(char x,int start1)
  • 一串IndexOf(字符x,整数开始1,整数开始2)
  • 一串IndexOf(字符串s1)
  • 一串IndexOf(字符串s1,int start1)
  • 一串IndexOf(字符串s1、int start1、int start2)
  • 一串IndexOf(字符串s1、int start1、int start2、字符串比较cType)
  • 一串IndexOf(字符串s1、int start1、字符串比较cType)
  • 一串IndexOf(字符串s1,字符串比较cType)

一串IndexOf(char x)方法

此方法返回字符串中第一次出现的指定字符的从零开始的索引。如果没有找到这样的字符,则返回-1。

语法:

public int IndexOf(char x)
  • 参数: 此方法接受一个参数 char x 类型 系统烧焦 它指定了要搜索的字符。
  • 返回类型: 此方法的返回类型为 系统Int32 .

例子: 在下面的代码中,用户希望知道指定字符串“geeksforgeks”中字符“F”的索引,因此,该方法返回各自的结果,主要是字符“F”的第一次出现。同样在第二种情况下,字符“C”不存在,因此它只返回-1。

// C# program to illustrate the
// String.IndexOf(char x) method
using System;
namespace ConsoleApplication1 {
class Geeks {
// Main Method
static void Main( string [] args)
{
string str = "GeeksForGeeks" ;
// Finding the index of character
// which is present in string and
// this will show the value 5
int index1 = str.IndexOf( 'F' );
Console.WriteLine( "The Index Value of character 'F' is " + index1);
// Now finding the index of that character which
//  is not even present with the string
int index2 = str.IndexOf( 'C' );
// As expected, this will output value -1
Console.WriteLine( "The Index Value of character 'C' is " + index2);
}
}
}


输出:

The Index Value of character 'F' is 5
The Index Value of character 'C' is -1

一串IndexOf(char x,int start1)方法

此方法返回字符串中第一次出现的指定字符的从零开始的索引。但是,对该字符的搜索将从指定位置开始,如果未找到,则返回-1。

语法:

public int IndexOf(char x, int start1)
  • 参数: 该方法采用两个参数,即 char x 类型 系统烧焦 它指定了要搜索的字符并 start1 类型 系统Int32 以整数值的形式指定开始搜索的位置。
  • 返回类型: 此方法的返回类型为 系统Int32 .
  • 例外情况: 这种方法可以提供 ArgumentOutOfRange异常 如果 start1 小于0(零)或大于字符串的长度。

例子: 在下面的代码中,用户希望知道指定字符串“HelloGeeks”中字符“H”的索引,因此,此方法返回字符“H”的相应索引。然而如果 start1 如果大于1,则返回-1是显而易见的。

// C# program to illustrate the
// String.IndexOf(char x, int start1) method
using System;
namespace ConsoleApplication2{
class Geeks {
// Main Method
static void Main( string [] args)
{
string str = "HelloGeeks" ;
// Finding the index of character
// which is present in string
// this will show the value 0
int index1 = str.IndexOf( 'H' , 0);
Console.WriteLine( "The Index Value of character 'H' " +
"with start index 0 is " + index1);
// Now finding the index of character
// 'H' with starting position greater
// than index position of 'H'
int index2 = str.IndexOf( 'H' , 5);
// As expected, this will output value -1
Console.WriteLine( "The Index Value of character 'H' is " + index2);
}
}
}


输出:

The Index Value of character 'H' with start index 0 is 0
The Index Value of character 'H' is -1

一串IndexOf(char x,int start1,int start2)方法

此方法返回字符串中第一次出现的指定字符的从零开始的索引。但是,该角色的搜索将从指定位置开始 start1 直到指定位置,即 开始2 如果找不到,则返回-1。

语法:

public int IndexOf(char x, int start1, int start2)
  • 参数: 该方法采用三个参数,即 char x 类型 系统烧焦 指定了要搜索的字符, start1 类型 系统Int32 以整数值的形式指定开始搜索的位置 开始2 类型 系统Int32 指定要停止搜索的结束位置。
  • 返回类型: 此方法的返回类型为 系统Int32 .
  • 例外情况: 这种方法可以提供 ArgumentOutOfRange异常 如果 start1或start2为负值 start1大于当前字符串的长度 start2大于当前字符串的长度减去start1 .

例子: 在下面的代码中,用户希望知道指定字符串“我的生活我的规则”中字符“R”的索引,因此,此方法返回字符“R”的索引值。对于start1>1和start2<8的情况,它再次返回-1,因为它没有找到任何字符。

// C# program to illustrate the
// String.IndexOf(char x, int start1,
//  int start2) method
using System;
namespace ConsoleApplication3 {
class Geeks {
// Main Method
static void Main( string [] args)
{
string str = "My Life My Rules" ;
int index1 = str.IndexOf( 'R' , 2, 14);
// Here starting index is < Index value of 'R'
// Also ending index is > Index of 'R'
// Hence It is obvious to return 11
Console.WriteLine( "Index Value of 'R' with start" +
" Index =2 and end Index = 15 is " + index1);
// Now here starting index is chosen right
// However ending position is < index of 'R'
// Surely it will return -1
int index2 = str.IndexOf( 'R' , 1, 8);
Console.WriteLine( "Index Value of 'R' with start" +
" Index = 1 and end Index = 8 is " + index2);
}
}
}


输出:

Index Value of 'R' with start Index =2 and end Index = 15 is 11
Index Value of 'R' with start Index = 1 and end Index = 8 is -1

一串IndexOf(字符串s1)方法

此方法返回字符串中指定子字符串第一次出现的从零开始的索引。如果没有找到这样的字符串,则返回-1,与字符的情况相同。

语法:

public int IndexOf(string s1)
  • 参数: 此方法接受一个参数 s1 类型 系统一串 它指定了要搜索的子字符串。
  • 返回类型: 此方法的返回类型为 系统Int32 .如果找到该字符串,则为s1的从零开始的索引位置,如果未找到,则为-1。如果s1是字符串。为空,返回值为0。
  • 例外情况: 这种方法可以提供 无理例外 如果 s1 是空的。

例子: 在下面的代码中,已知字符串“How”出现在主字符串中,因此它只返回第一个字符的索引值。然而,在下一个例子中,没有名为“Chair”的子字符串,因此它只返回-1。

// C# program to illustrate the
// String.IndexOf(string  s1) method
using System;
namespace ConsoleApplication4 {
class Geeks {
// Main Method
static void Main( string [] args)
{
string str = "Hello Friends....How are you..." ;
int i = str.IndexOf( "How" );
// As this string is present in the
// main string then it will obviously
//  output the value as 17
Console.WriteLine( "First value Index of 'How' is " + i);
// now the following string is not present
// So as per the rules, it will return -1
int i1 = str.IndexOf( "Chair" );
// As this string is present in
// the main string then it will
// obviously output the value as -1
Console.WriteLine( "First value Index of 'Chair' is " + i1);
}
}
}


输出:

First value Index of 'How' is 17
First value Index of 'Chair' is -1
© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享