VB.NET Proper Case Issue?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SANDEEP PREMCHANDANI
    New Member
    • Aug 2010
    • 3

    VB.NET Proper Case Issue?

    Code:
            Dim strName As String
            strName = TextBox1.Text
    
            strName = StrConv(strName, vbProperCase)
    
            Label1.Text = strName
    My question is: If I give blank spaces before name after name and between name and surname/title, then I want to remove all blank spaces except one blank space between name and surname/title.

    Example : Input > " SANDEEP PREMCHANDANI "

    Output > I want "Sandeep Premchandani" (Single space between Sandeep and Premchandani)
  • richkid
    New Member
    • Apr 2007
    • 28

    #2
    Code:
    Dim strName, str, strUserName As String
            Dim arrName As String()
    
            strUserName = ""
            strName = TextBox1.Text
            arrName = strName.Split("bbb")
    
    
            For Each str In arrName
                If str.Length > 0 Then
                    strUserName += str & ""
                End If
            Next
    
            strUserName = StrConv(strUserName, vbProperCase)
            Label1.Text = strUserName
    Last edited by Frinavale; Aug 4 '10, 05:22 PM. Reason: Fixed code tags.

    Comment

    • SANDEEP PREMCHANDANI
      New Member
      • Aug 2010
      • 3

      #3
      Thanks a lot Mr. Richkid.

      Comment

      • richkid
        New Member
        • Apr 2007
        • 28

        #4
        glad to help

        Comment

        • MrMancunian
          Recognized Expert Contributor
          • Jul 2008
          • 569

          #5
          @richkid Perhaps next time, instead of just posting code, you can try to explain how to get to the solution? Now Sandeep can get back to work, but he doesn't know why this works and how to find it.

          Give a poor man a fish, he can eat for a day. Give him a fishingrod and he can catch food for the rest of his life.

          Steven

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            I don't know why @richkid is using the split method and then looping through things...you could just use the Regex.Replace method to replace anything that matches one or more white-space characters with one white-space character.

            For example:


            Code:
            Dim str As String = "A     bunch of         spaces  and     tabs       are inserted   through   out       this  String!"
            
            'Please note that "\s+" matches "one or more white space character"
            
            Dim cleanedStr As String = RegularExpressions.Regex.Replace(str, "\s+", " ")
            MessageBox.Show(str)
            MessageBox.Show(cleanedStr)
            -Frinny

            Comment

            Working...