string.join does not works for \0 char

Vivek Kumar 21 Reputation points
2021-08-12T12:25:41.763+00:00

string[] lines;
lines=File.ReadAllLines(pathfilenotepad);

......................EMS-SoftwareVersion-\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...........................

String.Join(",",lines)

fails after this does not join lines

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,386 Reputation points
    2021-08-12T12:38:03.883+00:00

    You could replace \0 first

    string[] lines = {
        "......................EMS-SoftwareVersion-\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...........................",
        "......................EMS-SoftwareVersion-\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0..........................."
    
    };
    
    var results = string.Join(",", lines.Select(x => x.Replace("\0", "")).ToArray());
    Console.WriteLine(results);
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,586 Reputation points
    2021-08-13T06:59:29.957+00:00

    Do you want to display \0 in the result string?

    If this is the case, then you can follow Karen's approach, the difference is to replace \0 with \\0.

           var results = string.Join(",", lines.Select(x => x.Replace("\0", "\\0")).ToArray());  
    

    This question has nothing to do with String.Join, but these characters are escaped.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments