C#删除字母和数字

时间:2017-03-26 14:42:27

标签: c#

如何从以*

开头的文本文件中读取随机行

我的文本文件有15000行。带有*字符的字符是问题,下面的行是与该问题相关的字词。

这是文件的一部分:

*Planete i njihovi sateliti
Zemlja=Mesec
Mars=Fobos
Jupiter=Io
Saturn=Titan
Uran=Titania
Neptun=Triton
Pluton=Haron
Merkur=Nema satelit

*Francuski gradovi
Bordeaux=Bordo
Auxerre=Okser
Toulouse=Tuluz
Nantes=Nant
Marseille=Marselj
Dijon=Dižon
Limoges=Limož
Chateauroux=Šatero

http://pastebin.com/u8q4rimX

2 个答案:

答案 0 :(得分:1)

将所有以*开头的行读入列表,然后获取随机索引 并在开头摆脱*

var questions = 
    File.ReadLines(filePath)
        .Where(line => line.StartsWith("*")).ToList();

var rng = new Random();
var myRandomQuestion = questions[rng.Next(questions.Count)].Substring(1);

<强>更新

如果你也需要以下几行,那么上面的工作就不行了 我首先建议你创建一些结构 例如:

public class QuestionAndTerms
{
    public string Question {get; set;}
    public List<string> Terms {get; set;}
}

然后,循环浏览文件并填写List<QuestionAndTerms>

var question = null;
foreach (string line in File.ReadLines(filePath))
{
    if (line.StartsWith("*"))
    {
        if (question!= null) 
        {
            // We have a previous question
            questionList.Add(question);
        }

        question = new QuestionAndTerms();
        question.Question = line.Substring(1);
    }
    if (!string.IsNullOrWhiteSpace(line))
    {
        question.Terms.Add(line);
    }
}

或使用StremReader

进行循环播放
using (var reader = File.OpenText(filePath))
{
    // loop and logic here

}

以上所有内容只是为了给出方向,既不测试也不完整。

答案 1 :(得分:0)

首先,您必须决定如何将问题和答案表示为数据结构。我建议用适当的类来表示问题和答案(我使用的是VS 2017 / C#7.0语法):

public class Choice
{
     public Choice (string choiceText, string answer)
     {
         ChoiceText = choiceText;
         Answer = answer;
     }

     public string ChoiceText { get; }
     public string Answer { get; }
}

public class Question
{
    public Question (string questionText)
    {
        QuestionText = questionText;
    }

    public string QuestionText { get; }
    public List<Choice> Choices { get; } = new List<Choice>();
}

现在您可以开始阅读文件并将问题和答案放入结构中

var questions = new List<Question>();
var Question lastQuestion = null;
foreach (string line in File.ReadLines(path)) {
    if (line.StartsWith("*")) {
        // We have a new question. Create a question object and add it to the list.
        lastQuestion = new Question(line.Substring(1));
        questions.Add(lastQuestion);
    } else if (lastQuestion != null) {
        // We have a term related to the last question. Add it to the last question.
        // Split the line at the equal sign.
        string[] parts = line.Split("=", StringSplitOptions.RemoveEmptyEntries);
        if (parts.Length > 0) {
            // We have at least one part that is not empty. Let’s assume it’s the part before
            // the "=". If we have a second part, insert it as well, otherwise insert null.
            var choice = new Choice(parts[0], parts.Length > 1 ? parts[1] : null);
            lastQuestion.Add(choice); 
        }
    }
}

但是,我注意到您的示例文件包含2种不同类型的问题。有些人在等号后有答案,而其他人有分开的选择和答案清单。因此,您必须扩展解决方案以考虑到这一点。

您的问题非常不明确,因此很难给您一个合适的答案。下一次,提前考虑并提出更具体,更精确的问题。此外,在Stack Overflow上,我们更愿意提供您到目前为止所尝试的代码示例。

相关问题