[英]Getting multiple substrings from single string C#
I'm using a string to represent name/value pairs in an image filename. 我正在使用字符串表示图像文件名中的名称/值对。
string pairs = image_attribs(color,purple;size,large).jpg;
I need to parse that string to get the name/value pairs from before and after the semicolon. 我需要解析该字符串以从分号之前和之后获取名称/值对。 I can split on the semicolon and subtract the length to the opening parenthesis, but I'd like the corresponding function to be scalable to multiple pairs. 我可以对分号进行拆分,并减去括号的长度,但是我希望相应的函数可扩展为多对。
I need to come up with a multiple substring function that can return those pairs. 我需要提出一个可以返回这些对的多重子串函数。 I will then make them a list of KeyValuePairs: 然后,我将它们作为KeyValuePairs的列表:
List<KeyValuePair<string, string>> attributes = new List<KeyValuePair<string, string>>();
The current parsing which gets only the first pair: 当前的解析仅获得第一对:
string attribs = imagepath.Substring(imagepath.IndexOf("(") +1, imagepath.IndexOf(";" - imagepath.IndexOf("(");
I already have the function to parse the comma-separated pairs to create and add new KeyValuePairs. 我已经具有解析逗号分隔对的功能,以创建和添加新的KeyValuePairs。
var repspl = mydata.Split(';').Select( x => new { Key = x.Split(',')[0], Value = x.Split(',')[1] });
You could do something fun, like: 您可以做一些有趣的事情,例如:
string pairs = "image_attribs(color,purple;size,large).jpg";
var attributes = Regex.Match(pairs, @"\((.*?)\)").Groups[1].Value.Split(';')
.Select(pair => pair.Split(','))
.Select(pair => new { Attribute = pair[0], Value = pair[1] });
You can use split function with an array like in this example : 您可以将split函数与以下示例中的数组一起使用:
using System;
public class SplitTest {
public static void Main() {
string words = "This is a list of words, with: a bit of punctuation" +
"\tand a tab character.";
string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });
foreach (string s in split) {
if (s.Trim() != "")
Console.WriteLine(s);
}
}
}
// The example displays the following output to the console:
// This
// is
// a
// list
// of
// words
// with
// a
// bit
// of
// punctuation
// and
// a
// tab
// character
This is from : http://msdn.microsoft.com/fr-fr/library/b873y76a(v=vs.110).aspx 这是从: http : //msdn.microsoft.com/fr-fr/library/b873y76a(v=vs.110).aspx
You can use a combination of a regex and a cool use of capture capabilities in the .net regex engine: 您可以结合使用正则表达式和.net正则表达式引擎中的捕获功能:
string pairs = "image_attribs(color,purple;size,large;attr,val).jpg";
//This would capture each key in a <attr> named group and each
//value in a <val> named group
var groups = Regex.Match(
pairs,
@"\((?:(?<attr>[^),]+?),(?<val>[^);]+?)(?:;|\)))*");
//Because each group's capture is stored in .net you can access them and zip them into one list.
var yourList =
Enumerable.Zip
(
groups.Groups["attr"].Captures.Cast<Capture>().Select(c => c.Value),
groups.Groups["val"].Captures.Cast<Capture>().Select(c => c.Value),
(attr, val) => new KeyValuePair<string, string>(attr, val)
).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.