Pulling sentences from text string

Posted on 2006-05-25
Last Modified: 2010-05-18
Is there a more compact way to get the first 3 sentences (if there are 3 sentences - some have less) from a paragraph of text?

                                                               strMessage = RemoveHTML(rsData("Message"))
                        nPos1 = InStr(strMessage, ".")
                        nPos2 = InStr(nPos1 + 1, strMessage, ".")
                        nPos3 = InStr(nPos2 + 1, strMessage, ".")

                        if nPos1 > 0 then
                             strMessage1 = Mid(strMessage, 1, nPos1)
                        end if
                        if nPos2 > nPos1 + 1 then
                              strMessage1 = Mid(strMessage, nPos1 + 1, nPos2 - nPos1)
                        end if
                        if nPos3 > nPos2 + 1 then
                              strMessage1 = Mid(strMessage, nPos2 + 1, nPos3 - nPos2)
                        end if
                        if strMessage1 <> "" then
                              strMessage = strMessage1
                        end if
Question by:ksg313
    LVL 54

    Accepted Solution


    There may be problems with this but you could try to use the Split command (  I would think that any problems the command would have would also happen in the code you are using now.  The split command will take a string and use a delimiter you specify (also a string) to create an array.  If you don't want the information in an array then the Split command would not work but I don't see why that would be a problem.  The split command can also take a count argument so that you can stop it after 3.  The code below should do what you want.

       arSentences = Split(strMessage, ". ", 3)

    I entered a space in the delimiter so that it would use that and not put it in the strings in the array.  If the sentences use 2 spaces then you could add another in the delimiter setting.  Another option would be to just use the period as the delimiter but then trim leading and trailing spaces.

    Let me know if you have questions or need more information.

    LVL 54

    Expert Comment

    I'm glad that I could help.  Thanks for the grade, the points and the fun question.


    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
    This demonstration started out as a follow up to some recently posted questions on the subject of logging in: and…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now