简体   繁体   English

提取字符串C#的一部分

[英]Extracting parts of a string c#

In C# what would be the best way of splitting this sort of string? 在C#中,分割这种字符串的最佳方法是什么?

%%x%%a,b,c,d

So that I end up with the value between the %% AND another variable containing everything right of the second %% ie var x = "x"; var y = "a,b,c,d" 这样我就得到了%%和另一个包含第二个%%的所有变量的变量之间的值,即var x = "x"; var y = "a,b,c,d" var x = "x"; var y = "a,b,c,d"

Where a,b,c.. could be an infinite comma seperated list. 其中a,b,c..可以是一个无限的逗号分隔列表。 I need to extract the list and the value between the two double-percentage signs. 我需要提取两个双百分号之间的列表和值。 (To combat the infinite part, I thought perhaps seperating the string out to: %%x%% and a,b,c,d . At this point I can just use something like this to get X. (为了对抗无限部分,我想也许可以将字符串分离为: %%x%%a,b,c,d 。在这一点上,我可以使用类似这样的东西来获得X。

var tag = "%%";
      var startTag = tag;
      int startIndex = s.IndexOf(startTag) + startTag.Length;
      int endIndex = s.IndexOf(tag, startIndex);
      return s.Substring(startIndex, endIndex - startIndex);

Would the best approach be to use regex or use lots of indexOf and substring to do the extracting based on te static %% characters? 最好的方法是使用正则表达式还是使用大量的indexOfsubstring基于静态%%字符进行提取?

Given that what you want is "x,a,b,c,d" the Split() function is actually pretty powerful and regex would be overkill for this. 鉴于您想要的是“ x,a,b,c,d”,Split()函数实际上非常强大,而正则表达式对此可能会过大。

Here's an example: 这是一个例子:

string test = "%%x%%a,b,c,d";
string[] result = test.Split(new char[] { '%', ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in result) {
  Console.WriteLine(s);
}

Basicly we ask it to split by both '%' and ',' and ignore empty results (eg. the result between "%%"). 基本上,我们要求它用'%'和','分开,并忽略空结果(例如,“ %%”之间的结果)。 Here's the result: 结果如下:

x
a
b
c
d

To Extract X : 提取X

If %% is always at the start then; 如果%%始终在开头;

string s = "%%x%%a,b,c,d,h";
s = s.Substring(2,s.LastIndexOf("%%")-2);
//Console.WriteLine(s);

Else; 其他;

string s = "v,u,m,n,%%x%%a,b,c,d,h";
s = s.Substring(s.IndexOf("%%")+2,s.LastIndexOf("%%")-s.IndexOf("%%")-2);
//Console.WriteLine(s);

If you need to get them all at once then use this; 如果您需要一次全部获取它们,请使用此功能。

string s = "m,n,%%x%%a,b,c,d";

var myList = s.ToArray()
              .Where(c=> (c != '%' && c!=','))
              .Select(c=>c).ToList();

This'll let you do it all in one go: 这将使您一次完成所有操作:

string pattern = "^%%(.+?)%%(?:(.+?)(?:,|$))*$";
string input = "%%x%%a,b,c,d";
Match match = Regex.Match(input, pattern);
if (match.Success)
{
    // "x"
    string first = match.Groups[1].Value;
    // { "a", "b", "c", "d" }
    string[] repeated = match.Groups[2].Captures.Cast<Capture>()
        .Select(c => c.Value).ToArray();
}

You can use the char.IsLetter to get all the list of letter 您可以使用char.IsLetter获取所有list of letter

string test = "%%x%%a,b,c,d";
var l = test.Where(c => char.IsLetter(c)).ToArray();
var output = string.Join(", ", l.OrderBy(c => c));

Since you want the value between the %% and everything after in separate variables and you don't need to parse the CSV, I think a RegEx solution would be your best choice. 由于您希望在%%和后面的所有内容之间使用单独的变量中的值,并且不需要解析CSV,因此我认为RegEx解决方案将是您的最佳选择。

var inputString = @"%%x%%a,b,c,d";
var regExPattern = @"^%%(?<x>.+)%%(?<csv>.+)$";

var match = Regex.Match(inputString, regExPattern);

foreach (var item in match.Groups)
{
    Console.WriteLine(item);                
}

The pattern has 2 named groups called x and csv , so rather than just looping, you can easily reference them by name and assign them to values: 该模式有两个名为xcsv命名组,因此,不仅可以循环,还可以通过名称轻松引用它们并将它们分配给值:

var x = match.Groups["x"];
var y = match.Groups["csv"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM