SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2014
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Strip out first word from string

    Hi Guys

    I hope this one is possible and if so, easy.

    I simply need to strip out the first word of a string. So, if I have the name "john smith", I need to display the name "john" on my page. I am currently using the following to pull in full name data from my database table:

    Code:
    <%=oRs("fullname")%>
    How would I display the first name (word) on my page?

    Any help would be fully appreciated.

    Best regards

    Rod from the UK

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Check out the Left and Instr functions, or the Split function.
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2014
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Ian

    Thanks so much for your response.

    Those functions didn't really help me. From what I could fine, they only allowed me to remover characters and not whole words.

    In the end I used the Split String function, for example:

    <%

    FullName = oRs("fullname") // ex. John Smith
    Name = Split(FullName, " ") // split with space, split function has 2 argument , first will be value and second will be parameter

    %>
    now you can read first parameter value by using array index.

    <%=Name(0) %> // returns John
    <%=Name(1) %> // returns Smith

    This seemed to work for me. There may be better ways but I couldn't find any

    Best regards

    Rod from the UK

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Which is what I said - look at the Split function

    Left with Instr would also have worked, but Split is probably faster and less memory-intensive depending on the length of the original string and how many spaces are in it.

    Code:
    FullName = oRs("fullname")
    sName = Left(FullName,Instr(FullName," ")-1)
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2014
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Ian

    Of course! Thanks so much for your help

    Best regards

    Rod from the UK


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
  •