如何快速修改以下字符串,高分求教

qxzhu 2016-09-23 05:14:06
string test="ABC+BB*2+CC*4";
修改规则:
test.add("ABC") 结果test="ABC*2+BB*2+CC*4";
test.add("BB") 结果test="ABC+BB*3+CC*4";
test.remove("ABC") 结果test=“BB*2+CC*4";
test.remove("BB") 结果test="ABC+BB+CC*4";

用什么方法会更好,谢谢!
...全文
122 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
its_good 2016-09-23
  • 打赏
  • 举报
回复
为什么总是在提交答复后,楼主已经结贴了!!!
its_good 2016-09-23
  • 打赏
  • 举报
回复
楼主的题目不合理吧: string test="ABC+BB*2+CC*4"; 修改规则: test.add("ABC") 结果test="ABC*2+BB*2+CC*4"; test.add("BB") 结果test="ABC+BB*3+CC*4"; test.remove("ABC") 结果test=“BB*2+CC*4"; test.remove("BB") 结果test="ABC+BB+CC*4"; 1、“ABC”、“BB”,这两项均添加一次,又删除了,不就等同于原多项式没变吗: test="ABC+BB*2+CC*4" 2、字符串虽然是引用类型,但其实现与值类型一样: 比如,替换功能: str = str.Replace("A","B"); 必须为字符串重新赋值。 3、这是原生的字符串,add为新方法,可使用扩展方法实现,这样所有字符串都会受影响,不推荐。 如果楼主觉得这样实现无所谓,那就到此结束。 创建个新类型,用楼上各位提供的方法实现如下:

public class MString
{
    //data container
    private Dictionary<string, int> _dictItem = null;

    public MString() : this(null) { }
    public MString(string data)
    {
        _dictItem = new Dictionary<string, int>();
        if (!String.IsNullOrWhiteSpace(data))
        {
            //split
            var arrPolyItem = data.Split(new char[] { '+' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in arrPolyItem)
            {
                var arrOneItem = item.Split(new char[] { '*' }, StringSplitOptions.RemoveEmptyEntries);
                int count = arrOneItem.Length == 2 ? int.Parse(arrOneItem[1]) : 1;
                if (count == 0)
                    continue;
                Add(arrOneItem[0], count);
            }
        }
    }
    //add an item
    public void Add(string key, int count = 1)
    {
        if (_dictItem.ContainsKey(key))
        {
            _dictItem[key] += count;
        }
        else
        {
            _dictItem.Add(key, count);
        }
    }
    //remove an item
    public void Remove(string key)
    {
        if (!_dictItem.ContainsKey(key))
            return;
        var count = _dictItem[key];
        count--;
        if (count == 0)
        {
            _dictItem.Remove(key);
        }
        else
        {
            _dictItem[key] = count;
        }
    }
    public override string ToString()
    {
        string strResult = String.Empty;
        foreach (var item in _dictItem)
        {
            strResult += "+" + item.Key + (item.Value == 1 ? "" : "*"+item.Value);
        }
        return strResult.Substring(1);
    }
}
调用:

MString mstring = new MString("ABC+BB*2+CC*4");
mstring.Add("ABC");
mstring.Add("BB");
mstring.Remove("ABC");
mstring.Remove("BB");
string result = mstring.ToString();  //ABC+BB*2+CC*4
qxzhu 2016-09-23
  • 打赏
  • 举报
回复
谢谢大家,搞定!结贴了。
  • 打赏
  • 举报
回复
var dic = "ABC+BB*2+CC*4".Split('+').Select(s =>
{
    var arr = s.Split('*');
    return arr;
}).ToDictionary(k => k[0], v => v.Length > 1 ? int.Parse(v[1]) : 1);
foreach (var kv in dic)
{
    Console.WriteLine(kv.Key + "   " + kv.Value);
}
  • 打赏
  • 举报
回复
格式固定的话,可以先split('+'),然后循环对每个字符串split('*'),如果split结果只有1个,表示没有多个
qxzhu 2016-09-23
  • 打赏
  • 举报
回复
看来用哪个都差不多,首先是得将这串字符串转成Dictionary或List 或数组。这一步比较麻烦。
qxzhu 2016-09-23
  • 打赏
  • 举报
回复
引用 3 楼 starfd 的回复:
string test="ABC+BB*2+CC*4"; 这个组织规则能改么?比如改成集合
List<string> list = new List<string>(){"ABC","BB","BB","CC","CC","CC","CC"}
这样无论你怎么操作,只是修改这个list集合,最后再group by下之后string.join合并成一个字符串
字符串中的元素不固定,但都是用+连起来,用 list好象也是个不错的选择。
qxzhu 2016-09-23
  • 打赏
  • 举报
回复
引用 2 楼 crystal_lz 的回复:
Dictionary<string,int> dic = new Dictionary<string,int>(); if(dic.ContaintsKey("ABC")) dic["ABC"]++; string strResult = string.Empty; foreach(var v in dic) strResult += v.Key + "*" + v.Value + "+"; strResult.Trim('+');
一会我试试这个,我原来是想用数组,将字符串split成数组,然后比较。不知道哪个效率会更高
  • 打赏
  • 举报
回复
string test="ABC+BB*2+CC*4"; 这个组织规则能改么?比如改成集合
List<string> list = new List<string>(){"ABC","BB","BB","CC","CC","CC","CC"}
这样无论你怎么操作,只是修改这个list集合,最后再group by下之后string.join合并成一个字符串
crystal_lz 2016-09-23
  • 打赏
  • 举报
回复
Dictionary<string,int> dic = new Dictionary<string,int>(); if(dic.ContaintsKey("ABC")) dic["ABC"]++; string strResult = string.Empty; foreach(var v in dic) strResult += v.Key + "*" + v.Value + "+"; strResult.Trim('+');
linrachel 2016-09-23
  • 打赏
  • 举报
回复
Dictionary<string,int> override ToString() ?

110,552

社区成员

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

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

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