Pregunta

Tengo una cadena C # " RIP-1234-STOP \ 0 \ 0 \ 0 \ b \ 0 \ 0 \ 0 ??? | B? Mp? \ 0 \ 0 \ 0 " regresó de una llamada a un conductor nativo.

¿Cómo puedo recortar todos los caracteres del primer terminador nulo '\ 0 \ en adelante? En este caso, solo me gustaría tener " RIP-1234-STOP " ;.

Gracias.

¿Fue útil?

Solución

Aquí hay un método que debería hacer el truco

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

  return input.Substring(0,index);
}

Otros consejos

Prueba esto:

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);

o simplemente:

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

Esto también funciona:

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 bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top