简体   繁体   中英

Extracting parts of a string c#

In C# what would be the best way of splitting this sort of string?

%%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"

Where a,b,c.. could be an infinite comma seperated list. 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.

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?

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.

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 :

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

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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