SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Getting First Word From A String

    Hey

    Im trying to get the first word from a string, so lets say I have two strings:
    dim strFirst as string = "Monday Tuesday Wednesday"
    dim strScond as string = "Hello world, I like weekends"

    - Is there a way to get the first word of these strings, so in the first case it would be 'Monday' and the second 'Hello'

    I have searched but havent been able to find anything.
    Turn My Text - íspuǝıɹɟ ʞooqǝɔɐɟ ɹnoʎ ǝzɐɯɐ puɐ ǝsnɟuoɔ

    Computer Game Cheats & Codes

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Answer:
    Dim sentence as String = "The Quick Brown Fox"
    Dim firstWord as String = sentence.Split(" ")(0)
    Turn My Text - íspuǝıɹɟ ʞooqǝɔɐɟ ɹnoʎ ǝzɐɯɐ puɐ ǝsnɟuoɔ

    Computer Game Cheats & Codes

  3. #3
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code VB:
    Dim firstWord as String = sentence.Substring(0, sentence.IndexOf(' '))

  4. #4
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trib4lmaniac View Post
    Code VB:
    Dim firstWord as String = sentence.Substring(0, sentence.IndexOf(' '))
    That'll fail when the string contains a single word with no spaces in it. You can include a "trick" and make sure there's always a space:

    Code VB:
    Dim firstWord as String = sentence.Substring(0, (sentence & " ").IndexOf(' '))

    (IIRC & is the concatenation operator in VB)

    You can also use a regular expression:

    Dim firstWord as String = Regex.Match(sentence, "^.+?\b").Value

    explanation of the regex:
    ^ matches the beginning, ensuring that the pattern can only be used at the start of the string
    .+? means one or more (+) of any character (.), but as few as possible (the ? modifier).
    \b means a "word boundary"

    Advantages of the regex is that it will also consider punctuations etc. as word delimiters.

  5. #5
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    honeymonster - appreciate the reply, particularly the regex tip

    Many thanks
    Turn My Text - íspuǝıɹɟ ʞooqǝɔɐɟ ɹnoʎ ǝzɐɯɐ puɐ ǝsnɟuoɔ

    Computer Game Cheats & Codes

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look, ma. No spaces.

    Code:
    Dim firstWord as String = sentence.Substring(0, (sentence & " ").IndexOf(' ')).Trim()
    I prefer C#, btw.
    Web Application Development & Maintenance


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •