question

VivekKumar-7980 avatar image
0 Votes"
VivekKumar-7980 asked TimonYang-MSFT answered

string.join does not works for \0 char

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


dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I can't reproduce this issue. Can you share enough code to reproduce this issue?

Text file
......................EMS-SoftwareVersion-\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...........................
This is a test
......................EMS-SoftwareVersion-\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...........................
This is a test
......................EMS-SoftwareVersion-\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...........................
This is a test
......................EMS-SoftwareVersion-\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...........................
This is a test

Code

 static void Main(string[] args)
 {
     string[] lines;
     lines = File.ReadAllLines(pathfilenotepad);
     string result = String.Join(",", lines);
    
     Console.WriteLine(result);
 }

Result

 ......................EMS-SoftwareVersion-\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...........................,This is a test,......................EMS-SoftwareVersion-\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...........................,This is a test,......................EMS-SoftwareVersion-\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...........................,This is a test,......................EMS-SoftwareVersion-\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...........................,This is a test



0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

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);
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.