在C#中,IndexOf()方法是字符串方法。此方法用於在字符串的當前實例內查找指定字符或字符串的首次出現的從零開始的索引。如果找不到字符或字符串,則該方法返回-1。可以通過向其傳遞不同的參數來重載該方法。
- String.IndexOf(char x)
- String.IndexOf(char x,int start1)
- String.IndexOf(char x,int start1,int start2)
- String.IndexOf(strings1)
- String.IndexOf(strings1,int start1)
- String.IndexOf(string s1,int start1,int start2)
- String.IndexOf(string s1,int start1,int start2,StringComparison cType)
- String.IndexOf(string s1,int start1,StringComparison cType)
- String.IndexOf(string s1,StringComparison cType)
String.IndexOf(char x) method
此方法返回字符串中指定字符首次出現的從零開始的索引。如果找不到這樣的字符,則返回-1。
用法:
public int IndexOf(char x)
- 參數:此方法采用System.Char類型的參數char x,該參數指定要搜索的字符。
- 返回類型:此方法的返回類型為System.Int32。
例:在下麵的代碼中,用戶希望了解指定字符串“GeeksForGeeks”中字符“ 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
String.IndexOf(char x, int start1) method
此方法返回字符串中指定字符首次出現的從零開始的索引。但是,將從指定位置開始搜索該字符,如果找不到,則返回-1。
用法:
public int IndexOf(char x, int start1)
- 參數:此方法采用兩個參數,即System.Char類型的char x(指定要搜索的字符)和System.Int32類型的start1(以整數值形式指定要從其開始搜索的位置)。
- 返回類型:此方法的返回類型為System.Int32。
- 異常:如果start1小於0(零)或大於字符串的長度,則此方法可以提供ArgumentOutOfRangeException。
例:在下麵的代碼中,用戶希望知道指定字符串“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
String.IndexOf(char x, int start1, int start2) method
此方法返回字符串中指定字符首次出現的從零開始的索引。但是,該字符的搜索將從指定位置start1開始,直到指定位置,即start2,如果找不到,則返回-1。
用法:
public int IndexOf(char x, int start1, int start2)
- 參數:此方法采用三個參數,即System.Char類型的char x(指定要搜索的字符),System.Int32類型的start1(以整數值形式指定要從其開始搜索的位置的起始位置)和System2類型的start2。 .Int32,它指定要停止搜索的結束位置。
- 返回類型:此方法的返回類型為System.Int32。
- 異常:如果start1或start2為負,或者start1大於當前字符串的長度,或者start2大於當前字符串的長度減去start1,則此方法可以提供ArgumentOutOfRangeException。
例:在下麵的代碼中,用戶希望知道指定字符串“ My Life My Rules”中字符“ 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
String.IndexOf(string s1) method
此方法返回字符串中指定子字符串首次出現的從零開始的索引。如果找不到這樣的字符串,則它返回-1,與使用字符的情況相同。
用法:
public int IndexOf(string s1)
- 參數:此方法采用System.String類型的參數s1,該參數指定要搜索的子字符串。
- 返回類型:此方法的返回類型為System.Int32。如果找到該字符串,則為s1的從零開始的索引位置,否則為-1。如果s1為String.Empty,則返回值為0。
- 異常:如果s1為null,則此方法可以提供ArgumentNullException。
例:在下麵的代碼中,已知字符串“ 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
相關用法
- C# Math.Max()用法及代碼示例
- C# Decimal.Add()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# SortedDictionary.Add()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自keshav_786大神的英文原創作品 C# | String.IndexOf() Method | Set – 1。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。