If I have a filter string like this (filter for FileDialog):
"Image Files (*.bmp, *.jpg)|*.bmp;*.jpg|All Files (*.*)|*.*"
Is there a function in C# that gives me a list of all extensions in this filter?
This would be
"*.bmp", "*.jpg" and "*.*"
You can do it with this regular expresion:
(?<Name>[^|]*)\|(?<Extension>[^|]*)\|?
Here is a sample code:
var regex = new Regex(@"(?<Name>[^|]*)\|(?<Extension>[^|]*)\|?");
var matches = regex.Matches(@"Image Files (*.bmp, *.jpg)|*.bmp;*.jpg|All Files (*.*)|*.*");
foreach (Match match in matches)
{
Debug.Print("Name: '{0}' Extension:'{1}'", match.Groups["Name"].Value, match.Groups["Extension"].Value);
}
I don't think so. However, it's easy enough to write one yourself.
public string[] getExtensions(string input) {
if (string.isNullOrEmpty(input) || input.indexOf('|') == -1) {
return null;
}
else {
List<string> returnValue = new List<string>();
string[] parts = input.Split('|')
for(int x = 1; x < input.Length; x+=2) {
foreach(string extension in parts[x].Split(',')) {
returnValue.Add(extension);
}
}
return returnValue.ToArray();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With