Results 1 to 14 of 14

Thread: [RESOLVED] [02/03] String.Split

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Resolved [RESOLVED] [02/03] String.Split

    Hello again everyone!

    In my program, the user will enter Full Names of a customer. Now, what I want to achieve is to Capitalise the customer's name (Uppercase), he / she is known by.
    So for example: My full names are Ron Peter, but people call me Ron. In this case, I want to convert only Ron to uppercase, and leave Peter in title case. This will produce the end result of:
    RON Peter

    But, if I was known as Peter, it should have looked like this:
    Ron PETER

    I thought to use String.Split here, but it either converts everything to uppercase, or throw me an index out of bounds exception.

    This is what I've tried:
    Code:
        Private Sub AppCAPITALISE(ByVal strName As String, ByVal intNameIndex As Integer) 'sub to convert & display end result
            Dim stArAppFName As String()
    
            stArAppFName = strName.Split(" ", intNameIndex)
            For intTemp As Integer = 0 To intNameIndex
                txtAppFullNames.Text = stArAppFName(intTemp).ToUpper
            Next intTemp
        End Sub
    
        Private Sub rdFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdFirst.Click, rdSecond.Click, rdThird.Click, rdFourth.Click 'how many names?
            Select Case DirectCast(sender, RadioButton).Name
                Case "rdFirst"
                    shAppCAP = 1
                Case "rdSecond"
                    shAppCAP = 2
                Case "rdThird"
                    shAppCAP = 3
                Case "rdFourth"
                    shAppCAP = 4
            End Select
            AppCAPITALISE(strFullNames, shAppCAP)
        End Sub
    
        Private Sub txtAppFullNames_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAppFullNames.LostFocus 'store text entered
            If Not txtAppFullNames.Text = String.Empty Then strFullNames = txtAppFullNames.Text
    
        End Sub
    The variables I used were:
    Code:
        Private shAppCAP As Short
        Private strFullNames As String
    Can anyone help me format the names appropriately, please?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [02/03] String.Split

    Its your loop. It will capitalise all words in teh string array from the first word to whatever is designated in the received argument for the function.

    You should be upper'ing only the item in the arrray that is passed in. And remember its zero based arrays and not 1 based.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Lively Member
    Join Date
    Jul 2007
    Posts
    78

    Re: [02/03] String.Split

    Hi

    ur loop looks like this

    vb Code:
    1. For intTemp As Integer = 0 To intNameIndex-1

    then it will not gives u error.

    Bye.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [02/03] String.Split

    Quote Originally Posted by mehul.pandya
    Hi

    ur loop looks like this

    vb Code:
    1. For intTemp As Integer = 0 To intNameIndex-1

    then it will not gives u error.

    Bye.
    Yes, as I posted its a zero based array not one based.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: [02/03] String.Split

    Thanx for your responses, at least I don't get any exception now.
    BUT, when intNameIndex = 1 it returns:
    RON PETER
    when intNameIndex = 2 it returns:
    PETER

    How can I only capitalise one name in the string only, and still show the other names?
    Like:
    RON Peter
    or
    Ron PETER

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [02/03] String.Split

    Something like this.
    Code:
    	Private Sub AppCAPITALISE(ByVal strName As String, ByVal intNameIndex As Integer) 'sub to convert & display end result
    		Dim stArAppFName As String()
    		stArAppFName = strName.Split(" "c)
    		txtAppFullNames.Text = stArAppFName(intTemp - 1).ToUpper
    	End Sub
    Last edited by RobDog888; Aug 7th, 2007 at 04:41 AM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [02/03] String.Split

    VB.NET Code:
    1. Private Function capitaliseName(fullName As String, knownName As String) As String
    2.   Dim names As String() = fullName.Split(" "c)
    3.   For i As Integer = 0 To names.Length - 1 Step 1
    4.     If (names(i) = knownName) Then _
    5.       names(i) = names(i).ToUpper()
    6.   Next
    7.   Return String.Join(names, " "c)
    8. End Function

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: [02/03] String.Split

    Didn't see your post Penagate, le met try it now

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [02/03] String.Split

    Ok, a more "complete" example.
    Code:
    	Private Sub AppCAPITALISE(ByVal strName As String, ByVal intNameIndex As Integer) 'sub to convert & display end result
    		Dim stArAppFName As String()
    		stArAppFName = strName.Split(" "c)
    		For i As Integer = 0 To stArAppFName.Length - 1
    			If i = intNameIndex Then
    				txtAppFullNames.Text = txtAppFullNames.Text & stArAppFName(i - 1).ToUpper
    			Else
    				txtAppFullNames.Text = txtAppFullNames.Text & stArAppFName(i - 1)
    			End If
    		Next
    	End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: [02/03] String.Split

    Penagate, it shows me that value of type char cannot be converted to 1 dimensional array of string, on this line:
    Code:
     Return String.Join(names, " "c)

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [02/03] String.Split

    Sorry I got them the wrong way round. Should be (" "c, names)

  12. #12
    Lively Member
    Join Date
    Jul 2007
    Posts
    78

    Re: [02/03] String.Split

    Hey Try This

    vb Code:
    1. Private Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
    2.         TextBox2.Text = ProperString(TextBox1.Text, 0)
    3.     End Sub
    4.     Private Function ProperString(ByVal strVal As String, ByVal istrPos2Cap As Integer) As String
    5.         Dim strArr() As String
    6.         Dim i As Integer
    7.         ProperString = ""
    8.  
    9.         strArr = strVal.Split(" ")
    10.         For i = 0 To strArr.Length - 1
    11.             If i = istrPos2Cap Then
    12.                 ProperString += " " & strArr(i).ToUpper
    13.             Else
    14.                 ProperString += " " & System.Globalization.CultureInfo.InstalledUICulture.TextInfo.ToTitleCase(strArr(i))
    15.             End If
    16.         Next i
    17.         ProperString = ProperString.Trim
    18.     End Function

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: [02/03] String.Split

    WOW, thank you guys!
    One of my problems was that some of my indexes were wrong as well, it's fixed.

    I asked for 1 solution, and ended up with three magnificent ways of doing this - Thank you guys, thank you!!

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: [02/03] String.Split

    OK, one very last question.
    How can I reset the text back to it's original casing?

    I've tried:
    Code:
        Private Sub rdNone_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdNone.Click
            Dim AppTI As TextInfo = New CultureInfo("en-US", False).TextInfo
            Dim newText As String = AppTI.ToTitleCase(txtAppFullNames.Text)
            txtAppFullNames.Text = newText
    
        End Sub
    And imported the System.Globalisation namespace.

    The text doesn't revert back to Title case, any help?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width