Create array of strings from a larger string after delimiting

  • A+
Category:Languages

I have a string input which looks like this var input = "AB-PQ-EF=CD-IJ=XY-JK". I want to know if there is a way using string.split() method in C# and LINQ such that I can get an array of strings which looks like this var output = ["AB-PQ", "PQ-EF", "EF=CD", "CD-IJ", "IJ=XY", "XY-JK"]. Currently I am doing the same conversion manually by iterating the input string.

 


Can you use a regex instead of split?

var input = "AB-PQ-EF=CD-IJ=XY-JK"; var pattern = new Regex(@"(?<![A-Z])(?=([A-Z]+[=-][A-Z]+))"); var output = pattern.Matches(input).Cast<Match>().Select(m => m.Groups[1].Value).ToArray(); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: