2017-03-26 49 views
-2

如何閱讀從startswith *C#刪除字母和數字

我的文本文件有15000個lines.Lines與*字符的問題,併線之下是與該問題有關條款文本文件隨機行。

這是文件的一個組成部分:

*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

+0

我不確定我是否正確理解你想要什麼,但也許你可以將所有以*開頭的行存儲在列表結構中,然後生成一個隨機數來訪問該列表中的項目。 –

+0

如何做到這一點? – Pavle

+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 

} 

以上所有的只是給一個方向,它既不測試,也不完整。

+0

工作很棒! – Pavle

+0

你能告訴我如何讀取myRandomQuestion下面的行 – Pavle

+0

每個問題有8行與他有關嗎?如何閱讀下面的隨機問題 – Pavle

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上,我們更傾向於提供迄今爲止嘗試過的代碼示例。