Pergunta

Eu tenho uma string C # "RIP-1234-STOP \ 0 \ 0 \ 0 \ b \ 0 \ 0 \ 0 ??? | B Mp \ 0 \ 0 \ 0?" Retornado de uma chamada para um nativo motorista.

Como posso cortar todos os personagens do primeiro nulo terminator '\ 0 \ em diante. Neste caso, eu só gostaria de ter "RIP-1234-STOP".

Graças.

Foi útil?

Solução

Aqui está um método que deve fazer o truque

string TrimFromZero(string input)
{
  int index= input.IndexOf('\0');
  if(index < 0)
    return input;

  return input.Substring(0,index);
}

Outras dicas

Tente isto:

var input = "RIP-1234-STOP\0\0\0\b\0\0\0???|B?Mp?\0\0\0";
var firstNull = input.IndexOf('\0');
var output = input.Substring(0, firstNull);

ou simplesmente:

var output = input.Substring(0, input.IndexOf('\0'));

Isso funciona muito:

var input = "RIP-1234-STOP\0\0\0\b\0\0\0???|B?Mp?\0\0\0";
var split = input.Split('\0');
var output = split[0];
Assert.AreEqual("RIP-1234-STOP", output);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top