110,534
社区成员
发帖
与我相关
我的任务
分享
List<string> list = new List<string>();
string s = "AZBZCZ";
string a = "Z";
if (s.Contains(a))
{
string strr = s.Replace(a, "");
int ac = (s.Length - strr.Length) / a.Length;//查询出Z在里面出现的次数
for (int i = 0; i < ac; i++)
{
int l = s.IndexOf(a);
if (l > -1)
{
s = s.Substring(0, l) + "2" + s.Substring(l + a.Length);
list.Add(s);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<List<char>> simchr = new List<List<char>>()
{
new List<char>() { '0', 'o' },
new List<char>() { '2', 'z' },
new List<char>() { 'g', 'q', '9' }
};
string source = "22az9";
string source1 = string.Join("", source.Select((x, i) => simchr.Any(y => y.Any(z => z == x)) ? "{" + simchr.Single(y => y.Any(z => z == x)).FirstOrDefault() + ":" + i.ToString() + "}" : x.ToString()));
IEnumerable<string> query = new string[] { source1 };
while (Regex.IsMatch(query.First(), @"\{\w:\d+\}"))
{
string s = Regex.Match(query.First(), @"\{(\w):\d+\}").Value;
string c = Regex.Match(query.First(), @"\{(\w):\d+\}").Groups[1].Value;
query = query.SelectMany(x => simchr.First(y => y.First().ToString() == c).Select(y => x.Replace(s, y.ToString())));
}
foreach (var item in query)
{
Console.WriteLine(item);
}
}
}
}
22a2g
22a2q
22a29
22azg
22azq
22az9
2za2g
2za2q
2za29
2zazg
2zazq
2zaz9
z2a2g
z2a2q
z2a29
z2azg
z2azq
z2az9
zza2g
zza2q
zza29
zzazg
zzazq
zzaz9
Press any key to continue . . .string strvalue = "AZBZCZg";
string a = "Z";
List<string> list = new List<string>();
for (int i = 0; i < strvalue.Length; i++)
{
if (strvalue[i].ToString().Equals(a))
{
string abc = strvalue.Substring(0, i) + "2" + strvalue.Substring(i + 1);//A2BZCZ AZB2CZ AZBZC2
list.Add(abc);
string strr = abc.Replace(a, "");
int ac = (abc.Length - strr.Length) / a.Length;//查询出Z在里面出现的次数
for (int t = 0; t < ac; t++)
{
int l = abc.IndexOf(a);
if (l > -1)
{
abc = abc.Substring(0, l) + "2" + abc.Substring(l + a.Length);
if (!list.Contains(abc))//数组里又相同的排除
{
list.Add(abc);
}
}
}
}
}
这样可以遍历出所有的,不过如果字符串输入的是"AZBZCZg" g在这里可以认为是9 ,那么对应的呢?怎么弄哦~!