用条件连接字符串 [英] Joining lines of strings with a condition

查看:94
本文介绍了用条件连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。



我必须读取具有固定行长度的.txt文件。认为这行长度为12.以下是此示例.txt:



-第一行

-second line

-第三行

继续

-fourh l .....



如果是lenght一条线比它的固定长度大,它被写入一条新线。



但我的问题是,我想加入这个继续的新行字符串。我想:



-第一行

-second line

-第三行继续

-fourh l ....



但我无法完成它。

编辑:contidion是,如果行以' - '字符开头,这是一个新行。如果没有,请加入他们。



任何帮助?



感谢现在..

Hello there.

I have to read a .txt file that has fixed line lenght. Think this line lenght is 12. Below is an example of this .txt :

-first line
-second line
-third line
continues
-fourh l.....

If lenght of a line is bigger than it's fixed lenght, it's written to a new line.

But my problem is, i want to join this new line strings that continues. I want :

-first line
-second line
-third line continues
-fourh l....

But i couldn't accomplish it.
Edit : The contidion is, if line starts with a '-' char, this is a new line. If not, join them.

Any help?

Thanks from now..

推荐答案

这是一个非常简单的任务:在每一步,你必须读取输入文件(逐行),收集和连接读取行,直到满足停止条件。停止条件是新的行开头(hiphen)或文件结束。在停止条件下,收集的行被写入输出文件(并且收集新行,如果有的话)。
That is a pretty simple task: at each step you have to read the input file (line by line), collecting and concatenating the read lines until a stop condition is met. The stop condition is either a new start of line (the hiphen) or the end of file. Upon stop condition the collected lines are written to the output file (and the new line, if any, is collected).


这不是很复杂...

It's not exactly complex...
string[] input = new string[] {"-first line",
                               "-second line",
                               "-third line ",
                               "continues",
                               "-fourh l.....",};
List<string> list = new List<string>();
string old = "";
foreach (string s in input)
    {
    string line = s;
    if (!line.StartsWith("-"))
        {
        line = string.Format("{0}{1}", old, line);
        }
    list.Add(line);
    old = line;
    }
input = list.ToArray();


只需使用File.ReadAllText [ ^ ]。



然后,您可以执行一些简单的搜索和替换操作来转换刚刚读取的数据:
You can make this easier by just reading the entire file into a string using File.ReadAllText [^].

Then, you can perform some simple search-and-replace operations to transform the data you just read:
private string SampleData = @"-first line
-second line
-third line
continues
-fourh l";

private string replaceString = "-";

private string MassageData(string theData)
{
    theData = theData.Replace(Environment.NewLine, " ");
    
    // use this to remove the 'replaceString
    // theData = theData.Replace(replaceString, Environment.NewLine);

    // use this to keep the 'replaceString
    theData = theData.Replace(replaceString, Environment.NewLine + replaceString);
     
    // optional removal of extra spaces ...
    theData = theData.Replace("  ", " ");
    theData = theData.Trim();
}

// test
private void TestTransformData()
{
    string transformedData = MassageData(SampleData);
    Console.WriteLine(transformedData);
}


这篇关于用条件连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆