• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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