C#-在字符串中存储多个值

C#-在字符串中存储多个值,c#,C#,我是C#的新手,所以请对我放松点。。。基本上,我正在读取一个文本文件,希望获取两个字符串之间的任何数据,并将所有这些数据存储到一个字符串/变量中—我将如何执行此操作 我已经得到了下面的信息,但只需要朝着正确的方向推一下就可以让我回到正轨 非常感谢 C#代码: bool DetailsFound = false; string[] ReadLines = File.ReadAllLines(path); foreach (string item in ReadLines) { if (it

我是C#的新手,所以请对我放松点。。。基本上,我正在读取一个文本文件,希望获取两个字符串之间的任何数据,并将所有这些数据存储到一个字符串/变量中—我将如何执行此操作

我已经得到了下面的信息,但只需要朝着正确的方向推一下就可以让我回到正轨

非常感谢

C#代码:

bool DetailsFound = false;
string[] ReadLines = File.ReadAllLines(path);
foreach (string item in ReadLines) {
    if (item == "Start Details") {
        DetailsFound = true;
    }
    if (DetailsFound) {
        Console.WriteLine("Details - " + item);
        // VARIABLE / STRING REQUIRED HERE
    }
    if (item == "End Details") {
        break;
    }
}
示例*.TXT文件

Start Details I am line 1 I am line 2 I am line 3 End Details 开始详细信息 我是一号线 我是二号线 我是三号线 结束细节
你需要按正确的顺序做每件事。
使用类似的方法来存储字符串

var detailsFound = false;
var readLines = File.ReadAllLines(path);
var stringB = new StringBuilder();
foreach (string item in readLines) {
    if (item == "Start Details") {
        detailsFound = true;
    }
    else if (item == "End Details") {
        break;
    }
    else if (detailsFound) {
        Console.WriteLine("Details - " + item);
        stringB.AppendLine(item);
    }
}

你需要按正确的顺序做每件事。
使用类似的方法来存储字符串

var detailsFound = false;
var readLines = File.ReadAllLines(path);
var stringB = new StringBuilder();
foreach (string item in readLines) {
    if (item == "Start Details") {
        detailsFound = true;
    }
    else if (item == "End Details") {
        break;
    }
    else if (detailsFound) {
        Console.WriteLine("Details - " + item);
        stringB.AppendLine(item);
    }
}

首先,您需要修正您的逻辑以正确处理结束(这是执行顺序的问题)

然后,使用
StringBuilder
连接字符串:

StringBuilder sb = new StringBuilder();

foreach (string item in ReadLines) {
    if (item == "Start Details") {
        DetailsFound = true;
    }
    else if (item == "End Details") {
        break;
    }
    else if (DetailsFound) {
        Console.WriteLine("Details - " + item);
        sb.AppendLine(item);
        // VARIABLE / STRING REQUIRED HERE
    }
}

string output = sb.ToString();
如果要使行具有单独的值,请使用
列表

List List=新列表();
foreach(读取行中的字符串项){
如果(项目==“开始详细信息”){
DetailsFound=true;
}
else if(项目==“结束详细信息”){
打破
}
else if(DetailsFound){
Console.WriteLine(“详细信息-”+项目);
列表。添加(项目);
//此处需要变量/字符串
}
}
//列表保存所有行

首先,您需要修正逻辑以正确处理端点(这是执行顺序的问题)

然后,使用
StringBuilder
连接字符串:

StringBuilder sb = new StringBuilder();

foreach (string item in ReadLines) {
    if (item == "Start Details") {
        DetailsFound = true;
    }
    else if (item == "End Details") {
        break;
    }
    else if (DetailsFound) {
        Console.WriteLine("Details - " + item);
        sb.AppendLine(item);
        // VARIABLE / STRING REQUIRED HERE
    }
}

string output = sb.ToString();
如果要使行具有单独的值,请使用
列表

List List=新列表();
foreach(读取行中的字符串项){
如果(项目==“开始详细信息”){
DetailsFound=true;
}
else if(项目==“结束详细信息”){
打破
}
else if(DetailsFound){
Console.WriteLine(“详细信息-”+项目);
列表。添加(项目);
//此处需要变量/字符串
}
}
//列表保存所有行

你离得不远

对代码进行一些小的修改:

List<string> results = new List<string>();

foreach (string item in ReadLines) 
{
    if (item == "Start Details") 
    {
        DetailsFound = true;
    }
    else if (item == "End Details") {
        break;
    }
    else if (DetailsFound) 
    {
        Console.WriteLine("Details - " + item);
        results.Add(item);
    }

}

你离得不远

对代码进行一些小的修改:

List<string> results = new List<string>();

foreach (string item in ReadLines) 
{
    if (item == "Start Details") 
    {
        DetailsFound = true;
    }
    else if (item == "End Details") {
        break;
    }
    else if (DetailsFound) 
    {
        Console.WriteLine("Details - " + item);
        results.Add(item);
    }

}
请尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace Oppgave3Lesson1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            string input = "";
            List<string> lines = new List<string>();
            StreamReader reader = new StreamReader(FILENAME);

            Boolean foundStart = false;
            while ((input = reader.ReadLine()) != null)
            {
                if (!foundStart)
                {
                    if(input.StartsWith("Start")) foundStart = true;
                }
                else
                {
                    if (input.StartsWith("End")) break;
                    lines.Add(input);
                }
            }

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
命名空间Oppgave3Lesson1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.txt”;
静态void Main(字符串[]参数)
{
字符串输入=”;
列表行=新列表();
StreamReader=新的StreamReader(文件名);
布尔值foundStart=false;
while((input=reader.ReadLine())!=null)
{
如果(!foundStart)
{
如果(input.StartsWith(“Start”))foundStart=true;
}
其他的
{
if(input.STARTSTWITH(“End”))中断;
行。添加(输入);
}
}
}
}
}
尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace Oppgave3Lesson1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            string input = "";
            List<string> lines = new List<string>();
            StreamReader reader = new StreamReader(FILENAME);

            Boolean foundStart = false;
            while ((input = reader.ReadLine()) != null)
            {
                if (!foundStart)
                {
                    if(input.StartsWith("Start")) foundStart = true;
                }
                else
                {
                    if (input.StartsWith("End")) break;
                    lines.Add(input);
                }
            }

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
命名空间Oppgave3Lesson1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.txt”;
静态void Main(字符串[]参数)
{
字符串输入=”;
列表行=新列表();
StreamReader=新的StreamReader(文件名);
布尔值foundStart=false;
while((input=reader.ReadLine())!=null)
{
如果(!foundStart)
{
如果(input.StartsWith(“Start”))foundStart=true;
}
其他的
{
if(input.STARTSTWITH(“End”))中断;
行。添加(输入);
}
}
}
}
}

如果您想在某个特定字符串之后输出行,我会这样做

const string _startIdentifier = "Start Details"
const string _endIdentifier = "End Details"

private bool detailsFound
private List<string> details

foreach (string item in File.ReadAllLines(path)) {
    if (!detailsFound && item.Equals(_startIdentifier) {
        detailsFound = true;
    }

    if (item.Equals(__endIdentifier){
        break;
    }

    if (detailsFound) {
        details.Add(item);
        Console.WriteLine("Details - " + item);
    }
}

这将创建一个类似“我是第1行,我是第2行,我是第3行”的字符串。当然,您可以使用
环境将删除器从逗号更改为新行。新行

如果您想在某个特定字符串之后输出行,我会这样做

const string _startIdentifier = "Start Details"
const string _endIdentifier = "End Details"

private bool detailsFound
private List<string> details

foreach (string item in File.ReadAllLines(path)) {
    if (!detailsFound && item.Equals(_startIdentifier) {
        detailsFound = true;
    }

    if (item.Equals(__endIdentifier){
        break;
    }

    if (detailsFound) {
        details.Add(item);
        Console.WriteLine("Details - " + item);
    }
}

这将创建一个类似“我是第1行,我是第2行,我是第3行”的字符串。当然,您可以使用
环境将删除器从逗号更改为新行。新行

项在foreach循环中定义,并引用读取行。 必须在循环之前定义一个字符串变量并附加每个项

string WriteDataHere = String.Empty; // <- 

bool DetailsFound = false;
string[] ReadLines = File.ReadAllLines(path);
foreach (string item in ReadLines) {
    if (item == "Start Details") {
    DetailsFound = true;
}
if (DetailsFound) {
    Console.WriteLine("Details - " + item);
    // VARIABLE / STRING REQUIRED HERE
    WriteDataHere += item; // <--
    }
    if (item == "End Details") {
        break;
    }
}

string writedahahere=string.Empty;// 项在foreach循环中定义,并引用读取行。
必须在循环之前定义一个字符串变量并附加每个项

string WriteDataHere = String.Empty; // <- 

bool DetailsFound = false;
string[] ReadLines = File.ReadAllLines(path);
foreach (string item in ReadLines) {
    if (item == "Start Details") {
    DetailsFound = true;
}
if (DetailsFound) {
    Console.WriteLine("Details - " + item);
    // VARIABLE / STRING REQUIRED HERE
    WriteDataHere += item; // <--
    }
    if (item == "End Details") {
        break;
    }
}
string writedahahere=string.Empty;// 几点意见:

  • 您应该查看MSDN以了解更多信息
  • 使用
    File.ReadAllLines
    将文件中的所有行加载到内存中。在这种情况下,我认为我们不想这样做,因为我们可能会在几行之后停止。考虑使用
我们为您提供了大量使用常用枚举的答案,这对于C#初学者来说是非常好的,因为其他语言应该对此很熟悉。我想向您展示一个强大的C#功能

您可以按以下方式执行任务:

    public static void TakeBetween(string filePath, string startText, string endText)
    {
        var data = File.ReadLines(filePath).SkipWhile(line => !line.Contains(startText)).Skip(1).TakeWhile(line => !line.Contains(endText));
        // Do whatever that is needed with data. It is of type IEnumerable<string>
    }
publicstaticvoidtakebetween(字符串文件路径、字符串开始文本、字符串结束文本)
{
var data=File.ReadLines(filePath).SkipWhile(line=>!line.Contains(startText)).Skip(1).TakeWhile(line=>!line.Contains(endText));
//对数据执行任何需要的操作。数据类型为IEnumerable
}
您可以使用所需的任何其他字符串查找方法替换
.Contains
。请注意,这不会将不必要的数据加载到内存中

相关LINQ功能:

    • 很少有评论:

      • 您应该查看MSDN以了解更多信息