我有一个* .txt文件,其结构如下:
"thing 1" "thing 2" "thing 3" "thing 4"
"thing 6" "thing 7" "thing 8" "thing 9"
"thing 10" "thing 11" "thing 12" "thing 13"
"thing 14" "thing 15" "thing 16" "thing 17"
...
如何从文本文件中提取它,这样我就可以得到一些字符串,值为1,东西2等。
我只能一次读取整个文件。还是有另一种方式,如何存储一些"数据库"数据(也许是excel文件?)?
答案 0 :(得分:1)
这是基本的C#文件IO,实际上并不是Xamarin或Android特有的。
请注意,有很多方法可以解决这个问题,这只是一种方式。
// File classes are all in System.IO
// read each line as a string, put it into a list
List<string> data = File.ReadLines(path)
foreach(string s in data) {
// s is one line of data
// use Regex to grab each string (System.Text.RegularExpressions)
foreach(Match match in Regex.Matches(inputString, "\"([^\"]*)\""))
// match.Value should be the individual value "thing 1", etc
// you can strip off the "" if you want and then put into your DB
// or whatever you want to do with it
}
}
答案 1 :(得分:1)
这是一个非正则表达式解决方案。这将为您提供一系列字符串。
using System;
using System.Linq;
using System.Reflection;
public class Program
{
public static void Main()
{
String input = "\"thing 1\" \"thing 2\" \"thing 3\" \"thing 4\"";
var result = input.Split(new char[]{'\"'}).Where(p=> !String.IsNullOrWhiteSpace(p));
foreach(string v in result)
Console.WriteLine(v);
}
}