Solved

Trimming a string to shorten at an unspecified position.

Posted on 2006-07-06
7
244 Views
Last Modified: 2010-04-16
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
Comment
Question by:Paul Kahl
  • 3
  • 3
7 Comments
 
LVL 5

Expert Comment

by:bastibartel
Comment Utility
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
 
LVL 5

Expert Comment

by:bastibartel
Comment Utility
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
 
LVL 3

Author Comment

by:Paul Kahl
Comment Utility
cStr is VB, but I get the gist... refactoring and testing now...
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 13

Accepted Solution

by:
devsolns earned 500 total points
Comment Utility
       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
 
LVL 3

Author Comment

by:Paul Kahl
Comment Utility
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
 
LVL 3

Author Comment

by:Paul Kahl
Comment Utility
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
 
LVL 5

Expert Comment

by:bastibartel
Comment Utility
Yeah sry, was missing a compiler. Take care,
Sebastian
0

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

11 Experts available now in Live!

Get 1:1 Help Now