C# 删除字母和数字

C# 删除字母和数字,c#,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=Ok

如何从开始使用的文本文件中读取随机行*

我的文本文件有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

将以
*
开头的所有行读入列表,然后获得一个随机索引。
并在开始时去掉
*

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;}
}
或使用
StremReader循环

using (var reader = File.OpenText(filePath))
{
    // loop and logic here

}

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

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

公共类选择
{
公共选择(字符串选项文本、字符串答案)
{
ChoiceText=ChoiceText;
答案=答案;
}
公共字符串选项文本{get;}
公共字符串答案{get;}
}
公开课问题
{
公共问题(字符串问题文本)
{
提问文本=提问文本;
}
公共字符串QuestionText{get;}
公共列表选项{get;}=new List();
}
现在,您可以开始阅读该文件,并将问题和答案放入结构中

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); 
        }
    }
}
var问题=新列表();
var-Question-lastQuestion=null;
foreach(File.ReadLines(路径)中的字符串行){
if(带(“*”)的行开始){
//我们有一个新问题。创建一个问题对象并将其添加到列表中。
lastQuestion=新问题(第1行);
问题。添加(最后一个问题);
}else if(lastQuestion!=null){
//我们有一个与最后一个问题相关的术语。将其添加到最后一个问题。
//在等号处分开这条线。
string[]parts=line.Split(“=”,StringSplitOptions.RemoveEmptyEntries);
如果(零件长度>0){
//我们至少有一个零件不是空的。让我们假设它是之前的零件
//“=”。如果我们有第二个部分,也插入它,否则插入null。
var choice=新选项(零件[0],零件长度>1?零件[1]:空);
最后一个问题。添加(选择);
}
}
}
但是,我注意到您的示例文件包含两种不同类型的问题。有些人在等号后面有答案,而另一些人则把选择和回答分开。因此,您必须扩展解决方案以考虑到这一点


你的问题很不具体,所以很难给你一个合适的答案。下次,提前想一想,问一个更具体、更准确的问题。此外,在堆栈溢出方面,我们更希望您提供一个代码示例,说明您迄今为止所尝试的内容。

我不确定我是否正确理解您想要的内容,但也许您可以将所有以*开头的行存储在列表结构中,然后生成一个随机数来访问该列表的项目。如何做到这一点?您迄今为止尝试了什么?您是否有一些数据结构,您想在其中存储问题?您是否只想获取问题或后面的行?只是问题!!你能告诉我如何阅读我的随机问题下面的行吗每个问题都有8行与他相关的行吗?如何阅读随机问题下面的行这就是为什么我在发布前的评论中问你是否需要这些行。。。我会更新我的答案给你一个方向哇,那很有趣,很好的答案。问题列表在哪里
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); 
        }
    }
}