Domanda

Ho una stringa C # & R; RIP-1234-STOP \ 0 \ 0 \ 0 \ b \ 0 \ 0 \ 0 ??? | B? Mp? \ 0 \ 0 \ 0 " restituito da una chiamata a un driver nativo.

Come posso tagliare tutti i caratteri dal primo terminatore null '\ 0 \ in poi. In questo caso, vorrei solo avere " RIP-1234-STOP " ;.

Grazie.

È stato utile?

Soluzione

Ecco un metodo che dovrebbe fare il trucco

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

  return input.Substring(0,index);
}

Altri suggerimenti

Prova questo:

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 semplicemente:

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

Anche questo funziona:

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top