当前位置:编程学习 > C#/ASP.NET >>

C#编程求助

Write an algorithm to accept two string and check whether the second string exists within the first string.for example,if the first string is"concatenation" and the second string is "cat",the algorithm should display "substring found at position 4 in the string"
补充:并且判断在哪个位子
		
答案:
void substring(string s1,string s2)
{
bool flag=false;
int posi;
for(int i=0;i<s1.size()-s2.size();i++)
{
for(int j=0;j<s2.size();j++)
{
if(s1[i+j]!=s2[j])
break;
}
if(j==s2.size())
flag=true;
posi=i;
}

if(flag)
cout<<"the posi is "<<posi<<endl;
else cout<<"can't find ";
}
我的英语很烂
文本大概意思是否
需要求一字符串是否在另一字符中吗?

答案补充
indexof
答案补充
2.查找子串
字符串.IndexOf(子串,查找其实位置) ;
字符串.LastIndexOf(子串) ;最后一次出现的位置
str.IndexOf("ab",0);

不存在则是-1 存在就会输出子字符串所在的索引号数
英文很烂,几乎看不懂,
把你要实现的功能说下~!
public void E(string baseString,string subString)
{
int index = baseString.IndexOf(subString);

Console.WriteLine(string.Format("substring found at position {0} in the string", index));
}
/**
* 写一个算法来判断一个字符串是否包含在另一个字符串中
* 如果包含则输出位置。如果不包含则输出不包含
*/
public void getContainsIndex(String fromString, String containsString)
{
int index= fromString.IndexOf(containsString);
if (index>=0)
{
Console.WriteLine("substring found at position " + (index + 1) + " in the string");
}
else
{
Console.WriteLine("the second string is not contained in the second string");
}

}

我的QQ:357485570
写入一个接受两个字符串的运算并且制止写入的第二个字符串推出程序时是第一个字符串的实例。如果第一个字符串是“concatenation”,第二个字符串是“cat”。那么运算的将显示“substring found at position 4 in the string”

上一个:c#编程问题
下一个:c#编程问题

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,