|
-
Aug 7th, 2007, 03:31 AM
#1
Thread Starter
Hyperactive Member
[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?
-
Aug 7th, 2007, 04:04 AM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 7th, 2007, 04:18 AM
#3
Lively Member
Re: [02/03] String.Split
Hi
ur loop looks like this
vb Code:
For intTemp As Integer = 0 To intNameIndex-1
then it will not gives u error.
Bye.
-
Aug 7th, 2007, 04:20 AM
#4
Re: [02/03] String.Split
 Originally Posted by mehul.pandya
Hi
ur loop looks like this
vb Code:
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 7th, 2007, 04:28 AM
#5
Thread Starter
Hyperactive Member
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
-
Aug 7th, 2007, 04:38 AM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 7th, 2007, 04:46 AM
#7
Re: [02/03] String.Split
VB.NET Code:
Private Function capitaliseName(fullName As String, knownName As String) As String
Dim names As String() = fullName.Split(" "c)
For i As Integer = 0 To names.Length - 1 Step 1
If (names(i) = knownName) Then _
names(i) = names(i).ToUpper()
Next
Return String.Join(names, " "c)
End Function
-
Aug 7th, 2007, 04:50 AM
#8
Thread Starter
Hyperactive Member
Re: [02/03] String.Split
Didn't see your post Penagate, le met try it now
-
Aug 7th, 2007, 04:54 AM
#9
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 7th, 2007, 04:54 AM
#10
Thread Starter
Hyperactive Member
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)
-
Aug 7th, 2007, 04:55 AM
#11
Re: [02/03] String.Split
Sorry I got them the wrong way round. Should be (" "c, names)
-
Aug 7th, 2007, 05:00 AM
#12
Lively Member
Re: [02/03] String.Split
Hey Try This
vb Code:
Private Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox2.Text = ProperString(TextBox1.Text, 0)
End Sub
Private Function ProperString(ByVal strVal As String, ByVal istrPos2Cap As Integer) As String
Dim strArr() As String
Dim i As Integer
ProperString = ""
strArr = strVal.Split(" ")
For i = 0 To strArr.Length - 1
If i = istrPos2Cap Then
ProperString += " " & strArr(i).ToUpper
Else
ProperString += " " & System.Globalization.CultureInfo.InstalledUICulture.TextInfo.ToTitleCase(strArr(i))
End If
Next i
ProperString = ProperString.Trim
End Function
-
Aug 7th, 2007, 05:22 AM
#13
Thread Starter
Hyperactive Member
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!!
-
Aug 7th, 2007, 06:03 AM
#14
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|