Parse style attribute collection using linq

江枫思渺然 提交于 2019-12-06 11:04:14

Try the following

string attributes = "fill:#e2b126;stroke:#010101;stroke-width:0.3177;stroke-miterlimit:10";
var map = attributes
  .Split(new []{';'}, StringSplitOptions.RemoveEmptyEntries)
  .Select(x => x.Split(new []{':'}, StringSplitOptions.RemoveEmptyEntries))
  .ToDictionary(p => p[0], p => p[1]);

Breakdown

The first Split call will return an array of String values where every entry is in the key:value format. The following Select call will convert every one of those entries into a string[] where the first element is the key and the second is the value. The ToDictionary call just expressly performs this mapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!