请教:检测一个字符串中是否包含指定的字符串
如题
谢谢!
问题点数:100、回复次数:9Top
1 楼csxtu(就为了混口饭吃还不行嘛(做人要后到))回复于 2005-10-31 09:31:20 得分 10
String.IndexOf 方法Top
2 楼net_lover(【孟子E章】)回复于 2005-10-31 09:31:51 得分 10
str1.IndexOf(str2)
or
str1.LastIndexOf(str2)Top
3 楼csxtu(就为了混口饭吃还不行嘛(做人要后到))回复于 2005-10-31 09:32:37 得分 20
[C#]
public int IndexOf(
string value
);
[C++]
public: int IndexOf(
String* value
);
[JScript]
public function IndexOf(
value : String
) : int;
参数
value
要查找的 String。
返回值
如果找到该字符,则为 value 的索引位置;如果未找到该字符,则为 -1。如果 value 为 Empty,则返回值为 0。
Top
4 楼jxufewbt(我的目标是5星)回复于 2005-10-31 09:33:22 得分 10
用String.IndexOf()方法和String.Substring()方法。Top
5 楼ye_zi(行到水穷处·坐看云起时)回复于 2005-10-31 10:09:00 得分 10
str1.IndexOf(str2)
str1.LastIndexOf(str2)
或是用
substring()
方法也可以
Top
6 楼songhtao(三十年孤独)回复于 2005-10-31 10:13:32 得分 10
// 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 occurences 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 occurences 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
*/
Top
7 楼zfhuangwei(追风小蛇)回复于 2005-10-31 10:20:56 得分 10
str1.IndexOf(str2)
str1.LastIndexOf(str2)Top
8 楼baobei7758(陵少)回复于 2005-10-31 10:31:26 得分 10
String.IndexOf()来判断..Top
9 楼xhan2000(popeye.net)回复于 2005-10-31 10:41:59 得分 10
if(str1.indexof(str2)>-1)
{
//存在
}Top




