Zmodyfikuj funkcję, aby ignorowało puste stringi w command private static string…
- User
Zmodyfikuj funkcję, aby ignorowało puste stringi w command
private static string FullCommandCrced(string[] command) { const char Separator = '\u0009'; // Tab character const char StartMarker = '\u0002'; // Start of Text (STX) const char EndMarker = '\u0003'; // End of Text (ETX)
var joinedString = string.Join(Separator, command) + Separator; var crc = CalculateCrc(joinedString); return $"{StartMarker}{joinedString}#{crc}{EndMarker}"; } - Assistant
To modify the
FullCommandCrcedfunction so that it ignores empty strings in thecommandarray, you can use LINQ to filter out the empty strings before joining them. Here's how you can do it:private static string FullCommandCrced(string[] command) { const char Separator = '\u0009'; // Tab character const char StartMarker = '\u0002'; // Start of Text (STX) const char EndMarker = '\u0003'; // End of Text (ETX) // Filter out empty strings from the command array var nonEmptyCommand = command.Where(str => !string.IsNullOrEmpty(str)); // Join the non-empty strings and add the separator at the end var joinedString = string.Join(Separator, nonEmptyCommand) + Separator; var crc = CalculateCrc(joinedString); return $"{StartMarker}{joinedString}#{crc}{EndMarker}"; }Explanation:
-
Filtering Empty Strings: The
command.Where(str => !string.IsNullOrEmpty(str))line filters out any strings in the array that are either or empty ().
-