How do I do this with VB?

Discussion in 'ASP General' started by the other john, Jul 14, 2006.

  1. I'm trying to extract the first 3 words (or 4 or whatever) from a
    recordset field so I can format it with CSS. I'm not sure how to do
    this. mid() won't work because there's no way to know how many
    characters I'm dealing with.

    In short I what to dynamically retrieve the first few words from a
    field, format it with css, deliver it, then continue where it left off
    without the css.

    Any suggestions?

    Thanks!
    the other john, Jul 14, 2006
    #1
    1. Advertising

  2. the other john

    Mike Brind Guest

    the other john wrote:
    > I'm trying to extract the first 3 words (or 4 or whatever) from a
    > recordset field so I can format it with CSS. I'm not sure how to do
    > this. mid() won't work because there's no way to know how many
    > characters I'm dealing with.
    >
    > In short I what to dynamically retrieve the first few words from a
    > field, format it with css, deliver it, then continue where it left off
    > without the css.
    >


    Do you mean VBScript? Hope so...

    <%
    Dim str, i, spacepos, word
    Response.Write "<strong>"
    str = "My first three words in the sentence are bold"
    for i = 1 to 3
    spacepos = instr(str," ")
    word = left(str,spacepos)
    str = right(str,len(str)-spacepos)
    response.write word
    next
    Response.Write "</strong>"
    Response.Write str
    %>

    --
    Mike Brind
    Mike Brind, Jul 14, 2006
    #2
    1. Advertising

  3. Yes, VBScript.

    Assuming that there's no single function then to do this in ASP?

    Thanks Mike, Cool!

    Mike Brind wrote:
    > the other john wrote:
    > > I'm trying to extract the first 3 words (or 4 or whatever) from a
    > > recordset field so I can format it with CSS. I'm not sure how to do
    > > this. mid() won't work because there's no way to know how many
    > > characters I'm dealing with.
    > >
    > > In short I what to dynamically retrieve the first few words from a
    > > field, format it with css, deliver it, then continue where it left off
    > > without the css.
    > >

    >
    > Do you mean VBScript? Hope so...
    >
    > <%
    > Dim str, i, spacepos, word
    > Response.Write "<strong>"
    > str = "My first three words in the sentence are bold"
    > for i = 1 to 3
    > spacepos = instr(str," ")
    > word = left(str,spacepos)
    > str = right(str,len(str)-spacepos)
    > response.write word
    > next
    > Response.Write "</strong>"
    > Response.Write str
    > %>
    >
    > --
    > Mike Brind
    the other john, Jul 14, 2006
    #3
  4. You could use a regular expression. Somebody else will need to provide it if
    you can't come up with one via google.

    the other john wrote:
    > Yes, VBScript.
    >
    > Assuming that there's no single function then to do this in ASP?
    >
    > Thanks Mike, Cool!
    >
    > Mike Brind wrote:
    >> the other john wrote:
    >>> I'm trying to extract the first 3 words (or 4 or whatever) from a
    >>> recordset field so I can format it with CSS. I'm not sure how to do
    >>> this. mid() won't work because there's no way to know how many
    >>> characters I'm dealing with.
    >>>
    >>> In short I what to dynamically retrieve the first few words from a
    >>> field, format it with css, deliver it, then continue where it left
    >>> off without the css.
    >>>

    >>
    >> Do you mean VBScript? Hope so...
    >>
    >> <%
    >> Dim str, i, spacepos, word
    >> Response.Write "<strong>"
    >> str = "My first three words in the sentence are bold"
    >> for i = 1 to 3
    >> spacepos = instr(str," ")
    >> word = left(str,spacepos)
    >> str = right(str,len(str)-spacepos)
    >> response.write word
    >> next
    >> Response.Write "</strong>"
    >> Response.Write str
    >> %>
    >>
    >> --
    >> Mike Brind


    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
    Bob Barrows [MVP], Jul 14, 2006
    #4
  5. "Bob Barrows [MVP]" <> wrote in message
    news:...
    > You could use a regular expression. Somebody else will need to provide it

    if
    > you can't come up with one via google.
    >


    It's a bit tricky. This is the best I've come up with:-


    Dim rgx: set rgx = new RegExp

    rgx.Pattern = "((?:\b\w+\b.*?){3})(.*)?"

    sHTML = rgx.Replace(rst("MyField").value, "<strong>$1</strong>$2"

    It works when there are 3 or more words but fails when there are 0,1 or 2
    words in the string.





    > the other john wrote:
    > > Yes, VBScript.
    > >
    > > Assuming that there's no single function then to do this in ASP?
    > >
    > > Thanks Mike, Cool!
    > >
    > > Mike Brind wrote:
    > >> the other john wrote:
    > >>> I'm trying to extract the first 3 words (or 4 or whatever) from a
    > >>> recordset field so I can format it with CSS. I'm not sure how to do
    > >>> this. mid() won't work because there's no way to know how many
    > >>> characters I'm dealing with.
    > >>>
    > >>> In short I what to dynamically retrieve the first few words from a
    > >>> field, format it with css, deliver it, then continue where it left
    > >>> off without the css.
    > >>>
    > >>
    > >> Do you mean VBScript? Hope so...
    > >>
    > >> <%
    > >> Dim str, i, spacepos, word
    > >> Response.Write "<strong>"
    > >> str = "My first three words in the sentence are bold"
    > >> for i = 1 to 3
    > >> spacepos = instr(str," ")
    > >> word = left(str,spacepos)
    > >> str = right(str,len(str)-spacepos)
    > >> response.write word
    > >> next
    > >> Response.Write "</strong>"
    > >> Response.Write str
    > >> %>
    > >>
    > >> --
    > >> Mike Brind

    >
    > --
    > Microsoft MVP - ASP/ASP.NET
    > Please reply to the newsgroup. This email account is my spam trap so I
    > don't check it very often. If you must reply off-line, then remove the
    > "NO SPAM"
    >
    >
    Anthony Jones, Jul 14, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page