简体   繁体   中英

How to convert .txt file in resx to list<string>

so far:

List<string> names = Properties.Resources.first_names.ToArray().ToList();

produce wrong result,

the txt is like=> "Shirley","Rose","Sean","Jeremy"

Use a regex to split on the commas outside of the quotes like so:

var names = Regex.Split(Properties.Resources.first_names, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");

Then loop over each entry and remove the quotes like so:

for (int i = 0; i < names.Length; i++)
{
    names[i] = names[i]Replace("\"", "");
}

To read text file contents from resources and convert to list, try this:

byte[] file = Properties.Resources.myResourceFile;
string text;
using (Stream stream = new MemoryStream(file))
{
     using (StreamReader reader = new StreamReader(stream))
     {
          text = reader.ReadToEnd();
     }
}
var names = text.Split(',').ToList();

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