c#中,查找文本用indexof方法能不能指定从哪个位置开始查找的?如果不能,有其他方法吗?
3个回答
展开全部
不会就查msdn啊,
报告指定的 String 在此实例中的第一个匹配项的索引。搜索从指定字符位置开始,并检查指定数量的字符位置。
命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)
语法
Visual Basic(声明)
Public Function IndexOf ( _
value As String, _
startIndex As Integer, _
count As Integer _
) As Integer
Visual Basic(用法)
Dim instance As String
Dim value As String
Dim startIndex As Integer
Dim count As Integer
Dim returnValue As Integer
returnValue = instance.IndexOf(value, startIndex, count)
C#
public int IndexOf (
string value,
int startIndex,
int count
)
C++
public:
int IndexOf (
String^ value,
int startIndex,
int count
)
J#
public int IndexOf (
String value,
int startIndex,
int count
)
JScript
public function IndexOf (
value : String,
startIndex : int,
count : int
) : int
参数
value
要查找的 String。
startIndex
搜索起始位置。
count
要检查的字符位置数。
返回值
如果找到该字符,则为 value 的索引位置;如果未找到该字符,则为 -1。如果 value 为 Empty,则返回值为 startIndex。
异常
异常类型 条件
ArgumentNullException
value 为空引用(在 Visual Basic 中为 Nothing)。
ArgumentOutOfRangeException
count 或 startIndex 为负。
- 或 -
count 加 startIndex 之和指定一个不在此实例内的位置。
备注
索引编号从零开始。
此方法使用当前区域性执行单词(区分大小写和区域性)搜索。搜索从 startIndex 开始,一直搜索到 startIndex+count-1。位于 startIndex+count 的字符不包含在搜索范围内。
示例
下面的代码示例查找字符串“he”在另一个字符串的子字符串中的所有匹配项的索引。注意,对于每次搜索迭代,必须重新计算待搜索字符的个数。
Visual Basic 复制代码
' Sample for String.IndexOf(String, Int32, Int32)
Imports System
Class Sample
Public Shared Sub Main()
Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456"
Dim str As String = "Now is the time for all good men to come to the aid of their party."
Dim start As Integer
Dim at As Integer
Dim [end] As Integer
Dim count As Integer
[end] = str.Length
start = [end] / 2
Console.WriteLine()
Console.WriteLine("All occurrences of 'he' from position {0} to {1}.", start, [end] - 1)
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str)
Console.Write("The string 'he' occurs at position(s): ")
count = 0
at = 0
While start <= [end] AndAlso at > - 1
' start+count must be a position within -str-.
count = [end] - start
at = str.IndexOf("he", start, count)
If at = - 1 Then
Exit While
End If
Console.Write("{0} ", at)
start = at + 1
End While
Console.WriteLine()
End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'All occurrences of 'he' from position 33 to 66.
'0----+----1----+----2----+----3----+----4----+----5----+----6----+-
'0123456789012345678901234567890123456789012345678901234567890123456
'Now is the time for all good men to come to the aid of their party.
'
'The string 'he' occurs at position(s): 45 56
'
'
C# 复制代码
// Sample for String.IndexOf(String, Int32, Int32)
using System;
class Sample {
public static void Main() {
string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
string str = "Now is the time for all good men to come to the aid of their party.";
int start;
int at;
int end;
int count;
end = str.Length;
start = end/2;
Console.WriteLine();
Console.WriteLine("All occurrences of 'he' from position {0} to {1}.", start, end-1);
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
Console.Write("The string 'he' occurs at position(s): ");
count = 0;
at = 0;
while((start <= end) && (at > -1))
{
// start+count must be a position within -str-.
count = end - start;
at = str.IndexOf("he", start, count);
if (at == -1) break;
Console.Write("{0} ", at);
start = at+1;
}
Console.WriteLine();
}
}
/*
This example produces the following results:
All occurrences of 'he' from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
The string 'he' occurs at position(s): 45 56
*/
C++ 复制代码
// Sample for String::IndexOf(String, Int32, Int32)
using namespace System;
int main()
{
String^ br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
String^ br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
String^ str = "Now is the time for all good men to come to the aid of their party.";
int start;
int at;
int end;
int count;
end = str->Length;
start = end / 2;
Console::WriteLine();
Console::WriteLine( "All occurrences of 'he' from position {0} to {1}.", start, end - 1 );
Console::WriteLine( "{1}{0}{2}{0}{3}{0}", Environment::NewLine, br1, br2, str );
Console::Write( "The string 'he' occurs at position(s): " );
count = 0;
at = 0;
while ( (start <= end) && (at > -1) )
{
// start+count must be a position within -str-.
count = end - start;
at = str->IndexOf( "he", start, count );
if ( at == -1 )
break;
Console::Write( "{0} ", at );
start = at + 1;
}
Console::WriteLine();
}
/*
This example produces the following results:
All occurrences of 'he' from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
The string 'he' occurs at position(s): 45 56
*/
J# 复制代码
// Sample for String.IndexOf(String, Int32, Int32)
import System.*;
class Sample
{
public static void main(String[] args)
{
String br1 = "0----+----1----+----2----+----3----+----4----+----5----"
+ "+----6----+-";
String br2 = "01234567890123456789012345678901234567890123456789012345"
+ "67890123456";
String str = "Now is the time for all good men to come to the aid of "
+ "their party.";
int start;
int at;
int end;
int count;
end = str.get_Length();
start = end / 2;
Console.WriteLine();
Console.WriteLine("All occurrences of 'he' from position {0} to {1}.",
(Int32)start, (Int32)(end - 1));
Console.Write("{1}{0}", Environment.get_NewLine(), br1);
Console.Write("{1}{0}", Environment.get_NewLine(), br2);
Console.WriteLine("{1}{0}", Environment.get_NewLine(), str);
Console.Write("The string 'he' occurs at position(s): ");
count = 0;
at = 0;
while (start <= end && at > -1) {
// start+count must be a position within -str-.
count = end - start;
at = str.IndexOf("he", start, count);
if (at == -1) {
break;
}
Console.Write("{0} ", (Int32)at);
start = at + 1;
}
Console.WriteLine();
} //main
} //Sample
/*
This example produces the following results:
All occurrences of 'he' from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
The string 'he' occurs at position(s): 45 56
*/
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
报告指定的 String 在此实例中的第一个匹配项的索引。搜索从指定字符位置开始,并检查指定数量的字符位置。
命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)
语法
Visual Basic(声明)
Public Function IndexOf ( _
value As String, _
startIndex As Integer, _
count As Integer _
) As Integer
Visual Basic(用法)
Dim instance As String
Dim value As String
Dim startIndex As Integer
Dim count As Integer
Dim returnValue As Integer
returnValue = instance.IndexOf(value, startIndex, count)
C#
public int IndexOf (
string value,
int startIndex,
int count
)
C++
public:
int IndexOf (
String^ value,
int startIndex,
int count
)
J#
public int IndexOf (
String value,
int startIndex,
int count
)
JScript
public function IndexOf (
value : String,
startIndex : int,
count : int
) : int
参数
value
要查找的 String。
startIndex
搜索起始位置。
count
要检查的字符位置数。
返回值
如果找到该字符,则为 value 的索引位置;如果未找到该字符,则为 -1。如果 value 为 Empty,则返回值为 startIndex。
异常
异常类型 条件
ArgumentNullException
value 为空引用(在 Visual Basic 中为 Nothing)。
ArgumentOutOfRangeException
count 或 startIndex 为负。
- 或 -
count 加 startIndex 之和指定一个不在此实例内的位置。
备注
索引编号从零开始。
此方法使用当前区域性执行单词(区分大小写和区域性)搜索。搜索从 startIndex 开始,一直搜索到 startIndex+count-1。位于 startIndex+count 的字符不包含在搜索范围内。
示例
下面的代码示例查找字符串“he”在另一个字符串的子字符串中的所有匹配项的索引。注意,对于每次搜索迭代,必须重新计算待搜索字符的个数。
Visual Basic 复制代码
' Sample for String.IndexOf(String, Int32, Int32)
Imports System
Class Sample
Public Shared Sub Main()
Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456"
Dim str As String = "Now is the time for all good men to come to the aid of their party."
Dim start As Integer
Dim at As Integer
Dim [end] As Integer
Dim count As Integer
[end] = str.Length
start = [end] / 2
Console.WriteLine()
Console.WriteLine("All occurrences of 'he' from position {0} to {1}.", start, [end] - 1)
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str)
Console.Write("The string 'he' occurs at position(s): ")
count = 0
at = 0
While start <= [end] AndAlso at > - 1
' start+count must be a position within -str-.
count = [end] - start
at = str.IndexOf("he", start, count)
If at = - 1 Then
Exit While
End If
Console.Write("{0} ", at)
start = at + 1
End While
Console.WriteLine()
End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'All occurrences of 'he' from position 33 to 66.
'0----+----1----+----2----+----3----+----4----+----5----+----6----+-
'0123456789012345678901234567890123456789012345678901234567890123456
'Now is the time for all good men to come to the aid of their party.
'
'The string 'he' occurs at position(s): 45 56
'
'
C# 复制代码
// Sample for String.IndexOf(String, Int32, Int32)
using System;
class Sample {
public static void Main() {
string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
string str = "Now is the time for all good men to come to the aid of their party.";
int start;
int at;
int end;
int count;
end = str.Length;
start = end/2;
Console.WriteLine();
Console.WriteLine("All occurrences of 'he' from position {0} to {1}.", start, end-1);
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
Console.Write("The string 'he' occurs at position(s): ");
count = 0;
at = 0;
while((start <= end) && (at > -1))
{
// start+count must be a position within -str-.
count = end - start;
at = str.IndexOf("he", start, count);
if (at == -1) break;
Console.Write("{0} ", at);
start = at+1;
}
Console.WriteLine();
}
}
/*
This example produces the following results:
All occurrences of 'he' from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
The string 'he' occurs at position(s): 45 56
*/
C++ 复制代码
// Sample for String::IndexOf(String, Int32, Int32)
using namespace System;
int main()
{
String^ br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
String^ br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
String^ str = "Now is the time for all good men to come to the aid of their party.";
int start;
int at;
int end;
int count;
end = str->Length;
start = end / 2;
Console::WriteLine();
Console::WriteLine( "All occurrences of 'he' from position {0} to {1}.", start, end - 1 );
Console::WriteLine( "{1}{0}{2}{0}{3}{0}", Environment::NewLine, br1, br2, str );
Console::Write( "The string 'he' occurs at position(s): " );
count = 0;
at = 0;
while ( (start <= end) && (at > -1) )
{
// start+count must be a position within -str-.
count = end - start;
at = str->IndexOf( "he", start, count );
if ( at == -1 )
break;
Console::Write( "{0} ", at );
start = at + 1;
}
Console::WriteLine();
}
/*
This example produces the following results:
All occurrences of 'he' from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
The string 'he' occurs at position(s): 45 56
*/
J# 复制代码
// Sample for String.IndexOf(String, Int32, Int32)
import System.*;
class Sample
{
public static void main(String[] args)
{
String br1 = "0----+----1----+----2----+----3----+----4----+----5----"
+ "+----6----+-";
String br2 = "01234567890123456789012345678901234567890123456789012345"
+ "67890123456";
String str = "Now is the time for all good men to come to the aid of "
+ "their party.";
int start;
int at;
int end;
int count;
end = str.get_Length();
start = end / 2;
Console.WriteLine();
Console.WriteLine("All occurrences of 'he' from position {0} to {1}.",
(Int32)start, (Int32)(end - 1));
Console.Write("{1}{0}", Environment.get_NewLine(), br1);
Console.Write("{1}{0}", Environment.get_NewLine(), br2);
Console.WriteLine("{1}{0}", Environment.get_NewLine(), str);
Console.Write("The string 'he' occurs at position(s): ");
count = 0;
at = 0;
while (start <= end && at > -1) {
// start+count must be a position within -str-.
count = end - start;
at = str.IndexOf("he", start, count);
if (at == -1) {
break;
}
Console.Write("{0} ", (Int32)at);
start = at + 1;
}
Console.WriteLine();
} //main
} //Sample
/*
This example produces the following results:
All occurrences of 'he' from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
The string 'he' occurs at position(s): 45 56
*/
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
展开全部
String.IndexOf 方法 (Char, [startIndex], [count])
报告指定字符在此实例中的第一个匹配项的索引。搜索从指定字符位置开始,并检查指定数量的字符位置。
参数
value
要查找的 Unicode 字符。 对 value 的搜索区分大小写。
startIndex(Int32)
可选项,搜索起始位置。不设置则从0开始。
count(Int32)
可选项,要检查的字符位置数。
返回值
如果找到该字符,则为 value 的索引位置;否则如果未找到,则为 -1。
报告指定字符在此实例中的第一个匹配项的索引。搜索从指定字符位置开始,并检查指定数量的字符位置。
参数
value
要查找的 Unicode 字符。 对 value 的搜索区分大小写。
startIndex(Int32)
可选项,搜索起始位置。不设置则从0开始。
count(Int32)
可选项,要检查的字符位置数。
返回值
如果找到该字符,则为 value 的索引位置;否则如果未找到,则为 -1。
参考资料: http://baike.baidu.com/view/1674560.htm
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
String..::.IndexOf 方法 (Char, Int32, Int32),报告指定字符在此实例中的第一个匹配项的索引。搜索从指定字符位置开始,并检查指定数量的字符位置。刚查的,你问的那东西可以实现
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询