VB.NET - Remove double spaces in a textbox - Asked By Ben Sebuabe on 20-Sep-14 08:29 AM

I'm using vb.net 2013 and I have this textbox that users enter their names, but I realized that they enter their names by entering so many spaces I used the replace function but it does not work well. Is there anyway to prevent this at keypress, when the user tries to press the space bar more than once?
Robbe Morris replied to Ben Sebuabe on 20-Sep-14 08:30 AM
Put your replace inside of a while loop that checks for at least one instance of two spaces.  If it finds it, it goes inside the loop and performs the replace.  If it doesn't, it exits the loop.
Ben Sebuabe replied to Robbe Morris on 20-Sep-14 08:58 AM
Thanks for your urgent response. I will try that.
Robbe Morris replied to Ben Sebuabe on 20-Sep-14 09:30 AM
Forgot to put the code in (you'll need to convert to VB.NET):

var delimiter = "  ";
var file = "Robbe    Morris  is   a super neat guy.";

while (file.Contains(delimiter))

{

   file = file.Replace(delimiter, " ").Trim();

}

Debug.WriteLine(file);

Ben Sebuabe replied to Robbe Morris on 22-Sep-14 06:18 AM
Thanks a lot, it works!! Why is it that you are the only person responding to questions? God bless you, keep it up.
Robbe Morris replied to Ben Sebuabe on 22-Sep-14 07:41 AM
I own the site.  It is an old forum that I moved from another domain to get rid of nasty google penalties.  A lot of the old followers didn't come over.