当前位置: 代码迷 >> ASP.NET >> 简单的方法
  详细解决方案

简单的方法

热度:4080   发布时间:2013-02-25 00:00:00.0
求一个简单的方法
我有个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                 */
  相关解决方案