原字符串:
1:AddCategory,1:EditCategory,2:EditContent,3:AddCategory,3:CheckComment,3:AddLink
处理成:
1:AddCategory:EditCategory,2:EditContent,3:AddCategory:CheckComment:AddLink
JS和C#都可以。
C#
string.Join(",", "1:AddCategory,1:EditCategory,2:EditContent,3:AddCategory,3:CheckComment,3:AddLink".Split(',').GroupBy(str => str[0]).Select(g => g.Key + string.Join("", g.SelectMany(str => str.Substring(1)))))
大神就是厉害,但是如果字符串更长还是有问题。前面几行没有问题但是后面好像乱了。如:
3:AddCategory,3:DeleteCategory,3:EditCategory,3:CheckCategory,3:AddSpecial,3:DeleteSpecial,3:EditSpecial,4:AddCategory,4:DeleteCategory,4:EditCategory,4:CheckCategory,4:AddLink,4:DeleteLink,4:EditLink,4:AddSpecial,4:DeleteSpecial,4:EditSpecial,5:AddCategory,5:DeleteCategory,5:EditCategory,5:CheckCategory,5:AddContent,5:DeleteContent,5:EditContent,5:CheckContent,5:AddLink,5:DeleteLink,5:DeleteComment,5:CheckComment,5:DeleteGuestbook,5:CheckGuestbook,6:AddCategory,6:DeleteCategory,6:EditCategory,6:CheckCategory,6:AddSpecial,6:DeleteSpecial,15:AddCategory,15:DeleteCategory,15:EditCategory,15:CheckCategory,15:AddTemplate,15:EditTemplate,16:AddCategory,16:DeleteCategory,16:EditCategory,16:CheckCategory,16:EditContent,16:CheckContent,16:AddTemplate,16:EditTemplate,16:DeleteComment,16:CheckComment
@abc54288: 那就把 str[0]变成 str.Split(':')[0]
@长蘑菇星人: 这个我换过了,还是不行。
@abc54288: 当然啦。str.Substring(1) 换成什么 呢。
@长蘑菇星人: 搞定了。str.Substring(str.IndexOf(':'))
膜拜,这语句太6 了。
@abc54288: orz g.Key.Length
没看出是什么规则,处理个蛋蛋
JS
var strOld = "1:AddCategory,1:EditCategory,2:EditContent,3:AddCategory,3:CheckComment,3:AddLink";
var result ="";
strOld.split(",").forEach(function (item) {
var list=item.split(":");
result += (result.indexOf(list[0] + ":") == -1 ? ((result.length == 0 ? "" : ",")+ item) : (":" + list[1]));
});