Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 817161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:56:56+00:00 2026-05-15T01:56:56+00:00

Here is what I’m trying to accomplish. I have an object coming back from

  • 0

Here is what I’m trying to accomplish. I have an object coming back from
the database with a string description. This description can be up to 1000
characters long, but we only want to display a short view of this. So I coded
up the following, but I’m having trouble in actually removing the number of
words after the regular expression finds the total count of words. Does anyone
have good way of dispalying the words which are less than the Regex.Matches?

Thanks!

if (!string.IsNullOrEmpty(myObject.Description))
{
    string original = myObject.Description;
    MatchCollection wordColl = Regex.Matches(original, @"[\S]+");
    if (wordColl.Count < 70) // 70 words?
    {
        uxDescriptionDisplay.Text = 
             string.Format("<p>{0}</p>", myObject.Description);
    }
    else
    {                        
        string shortendText = original.Remove(200); // 200 characters?
        uxDescriptionDisplay.Text = 
              string.Format("<p>{0}</p>", shortendText);
    }
 }

EDIT:

So this is what I got working on my own:

else 
{
    int count = 0;
    StringBuilder builder = new StringBuilder();
    string[] workingText = original.Split(' ');
    foreach (string word in workingText)
    {
        if (count < 70)
        {
            builder.AppendFormat("{0} ", word);
        }
        count++;
    }
        string shortendText = builder.ToString();
}

It’s not pretty, but it worked. I would call it a pretty naive way of doing this. Thanks for all of the suggestions!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:56:57+00:00Added an answer on May 15, 2026 at 1:56 am

    One method, if you’re using at least C#3.0, would be a LINQ like the following. This is provided you’re going strictly by word count, not character count.

    if (wordColl.Count > 70)
    {
        foreach (var subWord in wordColl.Cast<Match>().Select(r => r.Value).Take(70))
        {
            //Build string here out of subWord
        }
    }
    

    I did a test using a simple Console.WriteLine with your Regex and your question body (which is over 70 words, it turns out).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is what I am trying to achieve in PHP: I have this string:
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the code in a function I'm trying to revise. This example works
Here is the css: #content ul { font-size: 12px; } I am trying this:
Here is the problem that I am trying to solve. I have two folders
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Here's my code: string strSQL = SELECT * from tMedia where SKU = '
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.