static void Main(string[] args)
{
int input = 0;
do
{
Console.WriteLine("请选择题目:");
Console.WriteLine("1-字符串包含,3-退出\r\n");
string inputtemp = Console.ReadLine();
try
{
input = Int32.Parse(inputtemp);
}
catch
{
Console.WriteLine("请选择题目:");
Console.WriteLine("1-字符串包含,3-退出\r\n");
inputtemp = Console.ReadLine();
}
switch (input)
{
case 1:
IsContain(); input = 0;
break;
}
{
int input = 0;
do
{
Console.WriteLine("请选择题目:");
Console.WriteLine("1-字符串包含,3-退出\r\n");
string inputtemp = Console.ReadLine();
try
{
input = Int32.Parse(inputtemp);
}
catch
{
Console.WriteLine("请选择题目:");
Console.WriteLine("1-字符串包含,3-退出\r\n");
inputtemp = Console.ReadLine();
}
switch (input)
{
case 1:
IsContain(); input = 0;
break;
}
} while (input != 3);
}
private static void IsContain()
{
int count = 0;//匹配的个数
int tempi = 0;//在字符1 中开始匹配的位置
string str1, str2;//字符串1 字符串2
bool result = false;//是否包含 默认 不包含
Console.WriteLine("Please enter 1st string Word:");
str1 = Console.ReadLine();
Console.WriteLine("Please enter 2ed string Word:");
str2 = Console.ReadLine();
{
int count = 0;//匹配的个数
int tempi = 0;//在字符1 中开始匹配的位置
string str1, str2;//字符串1 字符串2
bool result = false;//是否包含 默认 不包含
Console.WriteLine("Please enter 1st string Word:");
str1 = Console.ReadLine();
Console.WriteLine("Please enter 2ed string Word:");
str2 = Console.ReadLine();
//VS2008 系统自带 方法1
//if (str1.Contains(str2))
//{
// result = true;
//}
//VS2008 系统自带 方法2
//if (str1.IndexOf(str2) > -1)
//{
// result = true;
//}
//if (str1.Contains(str2))
//{
// result = true;
//}
//VS2008 系统自带 方法2
//if (str1.IndexOf(str2) > -1)
//{
// result = true;
//}
//VS2008 系统自带 方法3
//string[] strs = { str2 };
//if (str1.Split(strs, StringSplitOptions.None) > 1)
//{
// result = true;
//}
//string[] strs = { str2 };
//if (str1.Split(strs, StringSplitOptions.None) > 1)
//{
// result = true;
//}
//以下自己写的
//如果输入相等 则一定包含
if (str1 == str2)
{
result = true;
}
//只有 str1长度 大于等于 str2时 才有包含可能
if (str1.Length >= str2.Length)
{
char[] cstr1 = str1.ToCharArray();//转为字符处理
char[] cstr2 = str2.ToCharArray();
for (int i = 0; i < cstr1.Length; i++)//遍历str1
{
if (cstr1.Length - i >= cstr2.Length)
{//当 str1剩余的字符数 多于str2时 才有可能,否则不可能而跳出循环
count = cstr2.Length;//匹配的个数
tempi = i;//在字符1 中开始匹配的位置
for (int j = 0; j < cstr2.Length; j++)
{
//从字符1匹配处 开始比较 str2中的全部字符
if (cstr1[tempi] == cstr2[j])
{
count--;
tempi++;
}
else
{
break;
}
if (str1 == str2)
{
result = true;
}
//只有 str1长度 大于等于 str2时 才有包含可能
if (str1.Length >= str2.Length)
{
char[] cstr1 = str1.ToCharArray();//转为字符处理
char[] cstr2 = str2.ToCharArray();
for (int i = 0; i < cstr1.Length; i++)//遍历str1
{
if (cstr1.Length - i >= cstr2.Length)
{//当 str1剩余的字符数 多于str2时 才有可能,否则不可能而跳出循环
count = cstr2.Length;//匹配的个数
tempi = i;//在字符1 中开始匹配的位置
for (int j = 0; j < cstr2.Length; j++)
{
//从字符1匹配处 开始比较 str2中的全部字符
if (cstr1[tempi] == cstr2[j])
{
count--;
tempi++;
}
else
{
break;
}
}
if (count == 0)
{//说明 全部匹配
result = true;
count = cstr2.Length;//修正 匹配的个数
tempi = i;//修正 在字符1 中开始匹配的位置
break;
}
}
else
{
break;
}
if (count == 0)
{//说明 全部匹配
result = true;
count = cstr2.Length;//修正 匹配的个数
tempi = i;//修正 在字符1 中开始匹配的位置
break;
}
}
else
{
break;
}
}
}
}
if (result)//如果包含
{
string str = str1.Substring(0, tempi);
str += "(";//用括号 扩住包含的内容,当然只有第一个
str += str1.Substring(tempi, count);
str += ")";
if (tempi + count != str1.Length)
{
str += str1.Substring(count + tempi);
}
Console.WriteLine("输入的第一个字符串包含第二个字符串:{0}", str);
}
else
{
Console.WriteLine("输入的第一个字符串:{0}不包含第二个字符串:{1}", str1, str2);
{
string str = str1.Substring(0, tempi);
str += "(";//用括号 扩住包含的内容,当然只有第一个
str += str1.Substring(tempi, count);
str += ")";
if (tempi + count != str1.Length)
{
str += str1.Substring(count + tempi);
}
Console.WriteLine("输入的第一个字符串包含第二个字符串:{0}", str);
}
else
{
Console.WriteLine("输入的第一个字符串:{0}不包含第二个字符串:{1}", str1, str2);
}
}
}
转载于:https://blog.51cto.com/lichangming/349564