Replace returns the number of characters to replace found so you can try replacing two spaces with one space until no more space pairs are found:
while(sampleString.Replace(" "," "));
And then replace one space with underscore:
sampleString.Replace(" ",'_');