|
-
Dec 11th, 2007, 12:51 PM
#1
Thread Starter
New Member
[RESOLVED] String seperation
Hi there..
How do I seperate one string, into two seperate strings?
If i want to divide a Name, say Jack Johnson, into first name and last name, and write the first name in a seperate column, and the last name in a seperate column as well?
Thx for your time..
Bjede
-
Dec 11th, 2007, 12:53 PM
#2
Re: String seperation
Welcome to the forums. 
Code:
Dim arrName() As String
Dim strName As String
strName = "Jack Johnson"
arrName = Split(strName, " ")
Text1.Text = arrName(0)
Text2.Text = arrName(1)
-
Dec 11th, 2007, 12:57 PM
#3
Thread Starter
New Member
Re: String seperation
Hi Hack..
Thx for your quick response.. What if the name consists of a for, middle and a last name, and for- and middle name needs to "one" string..
/Bjede
-
Dec 11th, 2007, 12:58 PM
#4
Re: String seperation
They won't be one string, but you can display then as if they were.
If there are three names, then you will have arrName(0), arrName(1) and arrName(2)
-
Dec 11th, 2007, 01:00 PM
#5
Thread Starter
New Member
Re: String seperation
Super.. it works.. thx Hack..
-
Dec 11th, 2007, 01:01 PM
#6
Re: String seperation
Do you have control over how the name is being saved? If so, save the name in a format you can easily parse. Some names won't have middle initials, some may have 2 or more, some last names can be hyphenated, some first names are actually two names (like Jo Ann), some names are only initials (actor: D.B. Sweeney). There are many variations.
-
Dec 11th, 2007, 01:04 PM
#7
Re: String seperation
If you could have a name like "Eddie Van Halen" or "Frans van der Hoff" then you should modify Hack's code like this
Code:
Dim arrName() As String
Dim strName As String
Dim lngIndex As Long
strName = "Eddie Van Halen" 'Frans van der Hoff
arrName = Split(strName, " ")
Text1.Text = arrName(0)
Text2.Text = arrName(1)
For lngIndex = 2 To UBound(arrName)
Text2.Text = Text2.Text & " " & arrName(lngIndex)
Next
-
Dec 11th, 2007, 01:08 PM
#8
Re: String seperation
you could do this (slightly more complitcated) code that will always separate at the last space:
Code:
n$ = "Jack Johansen Johnson"
a = instrrev(n$, " ")
firstname = left(n$, a - 1)
lastname = right(n$, len(n$) - len(firstname) - 1
text1.text = firstname
text2.text = lastname
-
Dec 11th, 2007, 01:20 PM
#9
Re: String seperation
If you don't know how many names you will be dealing with (2 or 3) then try something like this
Code:
Private Sub Command1_Click()
Dim arrName() As String
Dim strName As String
strName = Text4.Text
arrName = Split(strName, " ")
Select Case UBound(arrName)
Case 2 'we have a middle name
Text1.Text = arrName(0) & " " & arrName(1)
Text2.Text = arrName(2)
Case 1 'we have no middle name
Text1.Text = arrName(0)
Text2.Text = arrName(1)
End Select
End Sub
-
Dec 11th, 2007, 03:49 PM
#10
Thread Starter
New Member
Re: String seperation
No, not really.. I do have some problems, when there are no middlename.. I need somehow to be able to know whether I have a middlename or not.. is it possible to do some sort of test, to see how many names there is in my array?
-
Dec 11th, 2007, 03:51 PM
#11
Thread Starter
New Member
Re: String seperation
just worked out.. thx guys..
-
Dec 12th, 2007, 10:20 AM
#12
Re: String seperation
 Originally Posted by Hack
If you don't know how many names you will be dealing with (2 or 3) then try something like this
Code:
Private Sub Command1_Click()
Dim arrName() As String
Dim strName As String
strName = Text4.Text
arrName = Split(strName, " ")
Select Case UBound(arrName)
Case 2 'we have a middle name
Text1.Text = arrName(0) & " " & arrName(1)
Text2.Text = arrName(2)
Case 1 'we have no middle name
Text1.Text = arrName(0)
Text2.Text = arrName(1)
End Select
End Sub
My code in post #7 does the same.
-
Dec 12th, 2007, 09:08 PM
#13
Re: String seperation
 Originally Posted by Bjede
No, not really.. I do have some problems, when there are no middlename.. I need somehow to be able to know whether I have a middlename or not.. is it possible to do some sort of test, to see how many names there is in my array?
you can get the upper bound of an array really easy
Code:
dim MyArray(1 to 5)
msgbox ubound(myarray)
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
|