Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

How to obtain the fist word from a given string

How can I programmitcally get the first word from a string if the format is the following?
Genesis: Chapter: 001
Exodus: Chapter: 001

I only need the first word, for example: Genesis or Exodus etc.


Node.Text is as follows: Genesis: Chapter: 001

Here is the code I'm using, it doesn't work and I've been working with this problem for hours.
    a% = Len(Trim(Node.Text))
    b% = 1
    Do Until a% = 0
        If Left$(Node.Text, b%) = ":" Then
            GoTo ExitDo
        Else
            Book_ID = Left$(Node.Text, b%)
        End If
        a% = a% - 1
        b% = b% + 1
    Loop
   
ExitDo:

Hint: I only need the string prior to the first colon.
0
mcdonald_g
Asked:
mcdonald_g
  • 4
  • 2
1 Solution
 
JR2003Commented:
Private Sub Command1_Click()
    Dim MyArr As Variant
    Dim MyString As String
    MyString = Node.Text
    MyString = Replace(MyString, ":", " ")
    MyArr = Split(MyString, " ")
    MsgBox MyArr(0)
End Sub
0
 
ampapaCommented:
Use the split function


dim strData

strData = split ("Genesis: Chapter: 001",":")

msgbox "First word is: " strdata(0)



   
 
0
 
JR2003Commented:
If you know it's always a text in front of the first colon it's even more straigh forward:

Private Sub Command1_Click()
    Dim MyArr As Variant
    MyArr = Split(Node.Text, ":")
    MsgBox MyArr(0)
End Sub
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ampapaCommented:
Even easier than that....    

a% = Len(Trim(Node.Text))
strData = split (Node.Text,":")
a% = strData(0)
0
 
ampapaCommented:
Sorry, goofed on the variable

a% = Len(Trim(Node.Text))
strData = split (a%,":")
a% = strData(0)
0
 
ampapaCommented:
hmmm...
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.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now