求助。。。对称字符串 列出一个字符串中所有对称的字串,如输入:“a^_^aXyZZzy123321",期望:“a^_^a","^_^","ZZ","33","2332", 代码不会了。拜托高手代码和思路 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 static void Main(string[] args) { string a = "a^_^aXyZZzy123321"; string[] subs = GetSubString(a); foreach (string subString in subs) { if (subString == new string(subString.ToCharArray().Reverse().ToArray())) Console.WriteLine(subString); } } public static string[] GetSubString(string s) { List<string> list = new List<string>(); for (int length = 2; length <= s.Length; length++) { for (int index = 0; index + length <= s.Length; index++) list.Add(s.Substring(index, length)); } return list.ToArray(); }哈哈,闲来无聊,帮楼主写了个 我也来一个 static void Main(string[] args) { string str = "a^_^aXyZZzy123321"; for (int iStart = 0; iStart < str.Length - 1; iStart++) { char head = str[iStart];//假设为头 int iFind = str.IndexOf(head, iStart + 1);//找尾 if (iFind > 0) { string strSub = str.Substring(iStart, iFind - iStart + 1); if (check(strSub)) { Console.WriteLine(strSub); continue; } } } Console.Read(); } static bool check(string str) { for (int i = 0, j = str.Length - 1; i < j; i++, j--) { if (str[j] != str[i]) { return false; } } return true; } public ArrayList GetValues(string s) { ArrayList al = new ArrayList(); for (int i = 0; i < s.Length; i++) { for (int j = 2; j < s.Length - i; j++) { //将对称值添加 if(GetResul(s.Substring(i, j))) { al.Add(s.Substring(i, j)); } else { } } } return al; } //判断是否对称 public bool GetResul(string s) { bool res = false; if (s.Length % 2 == 0) { for (int i = 0; i < s.Length / 2; i++) { if (s.Substring(i, 1) == s.Substring(s.Length-1 - i, 1)) { res = true; } else { res = false; } if (res == false) { return false; } } return res; } else { for (int i = 0; i < s.Length / 2+1; i++) { if (s.Substring(i, 1) == s.Substring(s.Length-1 - i, 1)) { res = true; } else { res = false; } if (res == false) { return false; } } return res; } } 请问这个正则怎么修改? 50分求一正则 Listview中如何绑定数据库中的数据? 字体大小问题--------------------想了很久都没解决,高手帮忙啊 提一个关于DATAGRID的简单问题!! 用鼠标在窗体写字 WebForm页面加载后,多次使用清缓存代码,IE无法加载页面 Socket双向心跳包问题 做过 WWF 的朋友进来看看.... 我怎么不能add web reference! 帮忙看看! 有什么方法可以在Gridview中显示别的表的列谢谢, 小弟最近在做毕业设计,遇到点问题,想请各位高手帮忙,我的题目是鲜花预订系统,但是删除鲜花时就会出现输入字符串格式不正确
static void Main(string[] args)
{
string a = "a^_^aXyZZzy123321"; string[] subs = GetSubString(a);
foreach (string subString in subs)
{
if (subString == new string(subString.ToCharArray().Reverse().ToArray()))
Console.WriteLine(subString);
}
} public static string[] GetSubString(string s)
{
List<string> list = new List<string>(); for (int length = 2; length <= s.Length; length++)
{
for (int index = 0; index + length <= s.Length; index++)
list.Add(s.Substring(index, length));
} return list.ToArray();
}
哈哈,闲来无聊,帮楼主写了个
{
string str = "a^_^aXyZZzy123321"; for (int iStart = 0; iStart < str.Length - 1; iStart++)
{
char head = str[iStart];//假设为头
int iFind = str.IndexOf(head, iStart + 1);//找尾
if (iFind > 0)
{
string strSub = str.Substring(iStart, iFind - iStart + 1);
if (check(strSub))
{
Console.WriteLine(strSub);
continue;
}
}
}
Console.Read();
} static bool check(string str)
{
for (int i = 0, j = str.Length - 1; i < j; i++, j--)
{
if (str[j] != str[i])
{
return false;
}
}
return true;
}
{
ArrayList al = new ArrayList();
for (int i = 0; i < s.Length; i++)
{
for (int j = 2; j < s.Length - i; j++)
{
//将对称值添加
if(GetResul(s.Substring(i, j)))
{
al.Add(s.Substring(i, j));
}
else
{ }
}
}
return al;
}
//判断是否对称
public bool GetResul(string s)
{
bool res = false;
if (s.Length % 2 == 0)
{
for (int i = 0; i < s.Length / 2; i++)
{
if (s.Substring(i, 1) == s.Substring(s.Length-1 - i, 1))
{
res = true;
}
else
{
res = false;
}
if (res == false)
{
return false;
}
}
return res;
}
else
{
for (int i = 0; i < s.Length / 2+1; i++)
{
if (s.Substring(i, 1) == s.Substring(s.Length-1 - i, 1))
{
res = true;
}
else
{
res = false;
}
if (res == false)
{
return false;
}
}
return res;
}
}