• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

Trimming a string to shorten at an unspecified position.

Given:
  1. variable length string, output from database
  2. Need to display no MORE than 80 characters
  3. Cannot end string on broken word (for instance, cutting at exactly 80 characters would be bad if it cuts a word in half)

The idea here is that I have a string from a field in a database. I want to trim it to 80 characters or as close to 80 without going over as I can get, without cutting any words in half.

To help, in ASP classic (which I'm converting from, to C#), I did it like this:

shortBody = Left(objRS(8), InStrRev(Left(objRS(8), 80), " "))


C# Only please.
0
Paul Kahl
Asked:
Paul Kahl
  • 3
  • 3
1 Solution
 
bastibartelCommented:
int pos=1000;

CString String( "...long text ...");

int pos = String.ReverseFind(' ');
while(pos>80)
{
   pos = String.ReverseFind(' ');
   if (pos>=0)
        String = String.Left(pos);
}
0
 
bastibartelCommented:
Sry, I clicked too soon,

CString String( "...long text ...");

int pos = String.ReverseFind(' ');
while(pos>80)
{
   String = String.Left(pos);
   pos    = String.ReverseFind(' ');
}
if (pos >0)
    return String.Left(pos);
else
   return "";

Oh, and I don't know, is CString C# ?
0
 
Paul KahlAuthor Commented:
cStr is VB, but I get the gist... refactoring and testing now...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
devsolnsCommented:
       public static string SpecialTrim(string value)
        {
            if (value.Length < 80)
                return value;
            //trim to 80
            value = value.Substring(0, 80);
            if (value.Substring(79, 1) != " ")//is last char a space?
            {
                int lastSpaceIndex = value.LastIndexOf(" ");
                if (lastSpaceIndex == -1)
                    return value;
                return value.Substring(0, lastSpaceIndex);
            }
            else
                return value;
        }
0
 
Paul KahlAuthor Commented:
Tested both of your answers, and discovered too many errors in Basti's to be able to easily refactor. Devsolns' answer was dead on, no need to refactor.

Thanks muchly for the help!
0
 
Paul KahlAuthor Commented:
Modified to take input of desired length:

public string RenderBodyTextShort(string inputString, int intDesiredStringLength)
    {
        if (inputString.Length <= intDesiredStringLength)
        {
            return inputString;
        }
        else
        {
            inputString = inputString.Substring(0, intDesiredStringLength);
            if (inputString.Substring(49, 1) != " ")
            {
                int lastSpaceIndex = inputString.LastIndexOf(" ");
                if (lastSpaceIndex == -1)
                    return inputString;
                return inputString.Substring(0, lastSpaceIndex);
            }
            else
            {
                return inputString;
            }
        }
    }
0
 
bastibartelCommented:
Yeah sry, was missing a compiler. Take care,
Sebastian
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now