新人求助一个程序问题

wgzboyx 2012-07-20 02:09:58
假设现在有这样一个字符串 w,z|a,b,g|z
想用循环把这个字符根据 '|'分割 ,","为每一位的几种情况。
希望得到的结果是
waz wbz wgz zaz zbz zgz
困扰了我好久,求大神帮忙解决
这个字符串实际程序里 长度是随机的,每一位有几种情况也是随机的
...全文
154 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wgzboyx 2012-07-20
  • 打赏
  • 举报
回复
非常感谢,小弟受益匪浅啊
全栈极简 2012-07-20
  • 打赏
  • 举报
回复
看来还是linq强大啊。
晚安苏州 2012-07-20
  • 打赏
  • 举报
回复
如果不用LINQ的话,也可以参考下我的方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace csdn_1
{
class Program
{

static void Main(string[] args)
{
string needTest = "w,z|a,b,c|z,x|a"; //举个例子;
List<string[]> save=new List<string[]>(); //泛型容器,用于保存下面划分的字符串数组;
string[] s1 = needTest.Split('|'); //先按“|”分割
for (int i = 0; i < s1.Length; i++)
{
string[] s2 = s1[i].Split(',');//再按“,”分割
save.Add(s2);//保存到泛型中去;
}
string[] result=DiGui(save);//利用下面的递归算法;最终得到一个你想要的一维字符串结果~
foreach (string item in result) //查看结果
{
Console.WriteLine(item+"\n");
}
Console.ReadLine();
}
/// <summary>
/// 这个方法的思路是,将划分好的相邻数组里的数据两两相组合,如按“|”划分后:数组1{w,z},数组2{a,b,g},数组3{z}:
/// 先将数组1的各数据和数组2个数据组合,形成:数组1{wa,wb,wg,za,zb,zg},数组2{z}
/// 先将数组1的各数据和数组2个数据组合,形成:{waz,wbz,wgz,zaz,zbz,zgz}
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static string[] DiGui(List<string[]> list)
{
int num = 0;
string[] _temp = new string[list[0].Length * list[1].Length];

for (int i = 0; i < list[0].Length; i++)
{
for (int j = 0; j < list[1].Length; j++)
{
_temp[num]=list[0][i]+list[1][j];
num++;
}
}
list.RemoveAt(0);//删除原先的第一个数组;
list[0] = _temp;//第一个数组改成新的组合数组;
if (list.Count > 1)//最后集合中肯定是只剩一个字符创数组了,也就是你要的结果;
{
DiGui(list);
}
return list[0];
}
}
}


iyomumx 2012-07-20
  • 打赏
  • 举报
回复
不用LINQ不舒服斯基:

var source = "a,b|C,D,E|f,g|H,I";
var resultList = (from unit in source.Split(new char[] { '|' }) select unit.Split(new char[] { ',' })).Aggregate(Enumerable.Repeat("", 1), (result, current) => (from o in result from t in current select o + t)).ToList();

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧