日期:2014-05-19  浏览次数:20825 次

怎样用C#判断一个字符串是否包含另一个字符串?简单吗?
这里涉及到中文和英文字符的混合的字符串比较!

------解决方案--------------------
indexof
------解决方案--------------------
indexof() 可以取到字符串的位置,应该可以不吧
------解决方案--------------------
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
label1.Text = textBox1.Text.IndexOf(textBox2.Text).ToString();

}
}
---------------------------------------
IndexOf找到返回位置,找不到返回-1
------解决方案--------------------
string a = "dahioh123geug ";

string b = "123 ";

if(a.IndexOf(b)> -1)

{

//字符串A中包含字符串B

}

------解决方案--------------------
string.IndexOf不行吗?楼主举两个例
------解决方案--------------------
static void Main(string[] args)
{
string t1 = "中华人民共和国 ";
string t2 = "华人 ";
if (t1.Contains(t2))
{
Console.WriteLine( "yes ");
}
else
{
Console.WriteLine( "no ");
}
}
运行结果:yes
------解决方案--------------------
就用IndexOf,和中英文没关系,别忘了,在C#.net中,都可以用中文命名变量呢!
------解决方案--------------------
// This example demonstrates the String.Contains() method
using System;

class Sample
{
public static void Main()
{
string s1 = "The quick brown fox jumps over the lazy dog ";
string s2 = "fox ";
bool b;
b = s1.Contains(s2);
Console.WriteLine( "Is the string, s2, in the string, s1?: {0} ", b);
}
}
/*
This example produces the following results:

Is the string, s2, in the string, s1?: True
*/

可以么?
------解决方案--------------------
IndexOf()函数包括了模式匹配,可以解决这个问题的
------解决方案--------------------
我的方法,使用正则表达式:
using System.Text.RegularExpressions;
string m = "我的网站地址www.10000o.com.cn ";
string n = Regex.Match(m, "地址www ").ToString();
if (n != " ")
{
//存在
}
else
{
//不存在
}


//验证通过