locked
c# Insert Break Line After Specific Character in Text File ? RRS feed

  • Question

  • Hello everyone,
    I need a code that will do something like break each line after a specific character (;), in fact the text after a specific character will go into a new line. Thanks in advance.

    string path = @"c:\File.txt";

    For example

    File.txt (multiline) before executing code:
    1234 ;Monday
    5678 ;Tuesday
    abcd ;Wednesday
    efgh ;Thursday
    0i9n ;Friday
    .
    .
    .

    File.txt (multiline) after executing the code:
    1234 ;
    Monday
    5678 ;
    Tuesday
    abcd ;
    Wednesday
    efgh ;
    Thursday
    0i9j ;
    Friday
    .
    .
    .

    Thursday, October 18, 2018 8:01 PM

Answers

  • Hi,

    you can use split(';') and after that use linq to get the ; back. array = array.Select(x => x + ";").ToArray() call File.AppendAllLines(path, array).

    Maybe like this:

                string path = @"c:\File.txt";
                var input = File.ReadAllText(path);
                string[] str = input.Split(';');
                str = str.Select(x => x + ";").ToArray();
                File.Delete(path);
                File.AppendAllLines(path, str);

    Or this for creating a new File:

                string path = @"c:\File.txt";
                string path2 = @"c:\File2.txt";
                var input = File.ReadAllText(path);
                string[] str = input.Split(';');
                str = str.Select(x => x + ";").ToArray();
                File.AppendAllLines(path2, str);

    It's not test by me yet...

    Greetings, Chris


    • Edited by DerChris88 Thursday, October 18, 2018 8:19 PM
    • Marked as answer by denysH Thursday, October 18, 2018 9:03 PM
    Thursday, October 18, 2018 8:13 PM

All replies

  • Hi,

    you can use split(';') and after that use linq to get the ; back. array = array.Select(x => x + ";").ToArray() call File.AppendAllLines(path, array).

    Maybe like this:

                string path = @"c:\File.txt";
                var input = File.ReadAllText(path);
                string[] str = input.Split(';');
                str = str.Select(x => x + ";").ToArray();
                File.Delete(path);
                File.AppendAllLines(path, str);

    Or this for creating a new File:

                string path = @"c:\File.txt";
                string path2 = @"c:\File2.txt";
                var input = File.ReadAllText(path);
                string[] str = input.Split(';');
                str = str.Select(x => x + ";").ToArray();
                File.AppendAllLines(path2, str);

    It's not test by me yet...

    Greetings, Chris


    • Edited by DerChris88 Thursday, October 18, 2018 8:19 PM
    • Marked as answer by denysH Thursday, October 18, 2018 9:03 PM
    Thursday, October 18, 2018 8:13 PM
  • string path = @"c:\File.txt";
    string path2 = @"c:\File2.txt";
    var input = File.ReadAllText(path)
        .Split(';')
        .Select(x => x + ";")
        .ToArray();
    File.AppendAllLines(path2, input);

    Thursday, October 18, 2018 8:35 PM
  • Hello, 

    Very difficult task. 

    Need all support from folks...

    string file1 = @".\Data\File1.txt";
    string file2 = @".\Data\File2.txt";
    File.WriteAllText(file2,File.ReadAllText(file1).Replace(";", ";\r\n"));

    string file1 = @".\Data\File1.txt";
    string file2 = @".\Data\File2.txt";
    
    char SpecificCharacter = ';'
    string LineBreak = "\r\n";
    
    File.WriteAllText(file2,File.ReadAllText(file1).Replace(String.Empty + SpecificCharacter, SpecificCharacter + LineBreak));

    LOF.


    Sincerely, Highly skilled coding monkey.


    • Edited by Andrey Belyakov Thursday, October 18, 2018 8:54 PM
    • Proposed as answer by DerChris88 Thursday, October 18, 2018 9:04 PM
    Thursday, October 18, 2018 8:48 PM
  • Thanks everyone, it works fantastic.
    Problem solved.
    Thursday, October 18, 2018 9:05 PM
  • I like Andrey's solution. Just replacing ";" by ";\r\n" is a bit nicer. But both should work...
    Thursday, October 18, 2018 9:07 PM