简体   繁体   English

如何从 c# 中的文本文件中读取特定的 UNPREDICTED 字符串

[英]How to read SPECIFIC UNPREDICTED string from a text file in c#

So I Have a.txt file that its content is like:所以我有一个 .txt 文件,其内容如下:

A = RANDOM STRING DETERMINED BY THE USER A =由用户确定的随机字符串

B = RANDOM STRING DETERMINED BY THE USER B =由用户确定的随机字符串

C = RANDOM STRING DETERMINED BY THE USER C =由用户确定的随机字符串

And want to get the string after "A = " + "B = " + "C = ".并想得到“A =”+“B =”+“C =”之后的字符串。

But also keep in mind that each string is RANDOM, so to get them you'll have to get' the string " A , B , C = " and then the string that is next to it would be the RANDOM UNPREDICTED string.但也要记住,每个字符串都是随机的,所以要得到它们,你必须得到字符串“ A , B , C = ” 然后它旁边的字符串将是 RANDOM UNPREDICTED 字符串。

Solution:解决方案:

string path = @"E:\1.txt";
string[] lines = File.ReadAllLines(path);
var result = lines.Select(s => s.Substring(4));

// Usage:
foreach (var s in result)
    Console.WriteLine(s);

// Output:
// 1234567
// 7854963
// aoeuidh

1.txt: 1.txt:

A = 1234567
B = 7854963
C = aoeuidh

Description:描述:

  • File.ReadAllLines Method:Opens a text file, reads all lines of the file into a string array, and then closes the file. File.ReadAllLines方法:打开一个文本文件,将文件的所有行读入一个字符串数组,然后关闭文件。

  • Enumerable.Select Method: Projects each element of a sequence into a new form. Enumerable.Select方法:将序列的每个元素投影到新形式中。

  • String.Substring(Int32) Method: Retrieves a substring from this instance. String.Substring(Int32)方法:从此实例中检索 substring。 The substring starts at a specified character position and continues to the end of the string. substring 从指定字符 position 开始,一直到字符串的末尾。

Do not forget using System.Linq;不要忘记using System.Linq; and using System.IO;using System.IO; . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM