简体   繁体   中英

Picking certain strings of an array with C#

我试图在数组中选择paticular字符串并将它们添加到一个新数组中,例如我想获取包含.txt和.rtf的数组中的所有字符串,并将它们添加到新数组中,例如filteredStrings []

You do not need regex for something that simple: Contains works faster, and is easier to understand:

var filteredStrings = myStrings.Where(s => s.Contains(".txt") || s.Contains(".rtf")).ToArray();

If you insist on using regex, you can do this:

var regexp = new Regex("[.](txt|rtf)");
var filteredStrings = myStrings.Where(s => regexp.IsMatch(s)).ToArray();
myArray.Where(x => Regex.IsMatch(x, @"\.(txt|rtf)$")).ToArray()

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