By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Remove double spaces in a textbox

P: 54
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 replace the double spaces . Thank you,
all.
Sep 20 '14 #1
Share this Question
Share on Google+
1 Reply


IronRazer
P: 82
Hi,
You can use RegularExpressions to find any spaces longer than 1 space and replace them with a single space. Try this.
Expand|Select|Wrap|Line Numbers
  1. Imports System.Text.RegularExpressions
  2.  
  3. Public Class Form1
  4.  
  5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  6.         TextBox1.Text = New Regex("\s+").Replace(TextBox1.Text, " ")
  7.     End Sub
  8. End Class
  9.  
You can check out the msdn link to the Regex.Replace method at the link below. It has an example of this also.

http://msdn.microsoft.com/en-us/libr...code-snippet-1
Sep 20 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.