how to find the first spca eon a srting (really eassy i just need urgent)

hi,
i have a string like this: Jhon Doe
how do i get to read that string and the until the space so in order words i need to get the part that display Jhon how do i do this?

i dont need a fix lenght of string because i have different records and all of them varies.
Thanks,
jsctechy
LVL 1
jsctechyAsked:
Who is Participating?
 
MikkkConnect With a Mentor Commented:
string1 = "Jhon doe"
msgbox(split(string1," ")(0))
0
 
MikkkCommented:
name=split("Jhon Doe"," ")(0)
surname=split("Jhon Doe"," ")(1)
0
 
mvidasCommented:
Hi jsctechy,

You can also use InStr() to determine the position of the first space, then use Left() to extract what comes before it:

 Dim oldStr As String, newStr As String, i As Long
 oldStr = "Jhon Doe"
 i = InStr(1, oldStr & " ", " ") 'append space after oldstr in case there is no space
 newStr = Left(oldStr, i - 1)

Matt
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
MikkkCommented:
Or you can do like this a (bit more complicated):

fullname="Jhon Doe"
name=left(fullname,instr(fullname," "))
surname=right(fullname,len(fullname)-instr(fullname," "))
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Or you could just check the value returned from Instr() and act accordingly:

    Dim str As String
    Dim spcIndex As Integer
    Dim value As String
   
    str = "Jhon Doe"
    spcIndex = InStr(str, " ")
    If spcIndex > 0 Then
        value = Left(str, spcIndex - 1)
    Else
        value = str ' return the whole string since no space was found
    End If
    Debug.Print value
0
 
jsctechyAuthor Commented:
i need something simple get the string and output it to message box
thats all i need

string = Jhon doe

msgbox =>Jhon
thats all i need
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.