c# - StringSplitOptions.RemoveEmptyEntries equivalent for TextFieldParser -
i learn textfieldparser parse words use string.split so. , have question regarding newly learned class.
if parse message using string.split stringsplitoptions.removeemptyentries
string message = "create myclass \"56, 'for better or worse'\""; //have multiple spaces string[] words = message.split(new char[] { ' ' }, 3, stringsplitoptions.removeemptyentries); then words contain 3 elements this:
[0] create [1] myclass [2] "56, 'for better or worse'" but if textfieldparser
string str = "create myclass \"56, 'for better or worse'\""; var parser = new microsoft.visualbasic.fileio.textfieldparser(new stringreader(str)); //treat string i/o parser.delimiters = new string[] { " " }; parser.hasfieldsenclosedinquotes = true; string[] words2 = parser.readfields(); then return consists of words without text
[0] create [1] [2] [3] [4] myclass [5] [6] [7] "56, 'for better or worse'" now there equivalent way remove empty words in resulting array string.split stringsplitoptions.removeemptyentries does?
may trick
parser.hasfieldsenclosedinquotes = true; string[] words2 = parser.readfields(); words2 = words2.where(x => !string.isnullorempty(x)).toarray(); one liner alternative be
string[] words2 = parser.readfields().where(x => !string.isnullorempty(x)).toarray();
Comments
Post a Comment