Получение пустой завершенной строки от Системы. Текст. Кодирование. Unicode. GetString

Используйте:

vertical-align: top;

Для текста

10
задан DougN 14 May 2009 в 16:07
поделиться

2 ответа

Если вы уверены, что все остальное - \ 0, это сработает:

cmd = cmd.TrimEnd('\0');

В противном случае, если вы просто хотите получить все до первого нуля:

int index = cmd.IndexOf('\0');
if (index >= 0)
   cmd = cmd.Remove(index);

Обратите внимание, что Unicode.GetString позаботится о двойных \ 0s. Вам просто нужно найти один \ 0.

13
ответ дан 3 December 2019 в 20:06
поделиться

The easiest way would be to trim the string after conversion, as already suggested.

If you know the character count in advance, you could use the GetString overload that takes a start index and a count of bytes, in order to get the correct string, without trimming.

If you do not know the character count in advance, and would like to avoid trimming the string afterwards, you need to trim the byte array first, so you only pass the bytes you are interested in. For Unicode, this would mean removing any bytes after and including the first pair of zeroes.

3
ответ дан 3 December 2019 в 20:06
поделиться
Другие вопросы по тегам:

Похожие вопросы: