日期:2014-05-18  浏览次数:20649 次

求一个简单的方法
我有个9位的字符串 全部是0和1 比如是000111100 然后0代表A,1代表B 最后要绑定成A0,A1,A2,B0,B1,B2,B3,A3,A4

------解决方案--------------------
string str = "000111100";
str = str.replace('0','A,');
str = str.replace('1','B,');
string[] s = str.split(',');
int i = 0;
int j = 0;
string m = "";
for(int x = 0; x < s.length; x++)
{
if (s[x] == "A")
{
m += s[x] + i.tostring() + ",";
i++;
}
else
{
m += s[x] + j.tostring() + ",";
j++;
)
}

m就是你要的信息
------解决方案--------------------
C# code
   string test_str = "000111100 ";
                List<string> temp_list = new List<string>();
                List<string> result_list = test_str.ToList().Select((a, i) =>
                {
                    string temp_save = null;
                    if (a == '0') 
                    {
                        temp_save = "A" + (temp_list.Where(b => b.Contains("A")).Count());
                        temp_list.Add("A");
                        return temp_save; 
                    } 
                    else {
                        temp_save = "B" + (temp_list.Where(b => b.Contains("B")).Count());
                        temp_list.Add("B");
                        return temp_save;
                    } 
                }).ToList();
                /*
                 *         [0]    "A0"    string
        [1]    "A1"    string
        [2]    "A2"    string
        [3]    "B0"    string
        [4]    "B1"    string
        [5]    "B2"    string
        [6]    "B3"    string
        [7]    "A3"    string
        [8]    "A4"    string
        [9]    "B4"    string

                 */