Solved

Trimming a string to shorten at an unspecified position.

Posted on 2006-07-06
7
256 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
ID: 17053214
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
ID: 17053257
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
ID: 17053304
cStr is VB, but I get the gist... refactoring and testing now...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Accepted Solution

by:
devsolns earned 500 total points
ID: 17053322
       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
ID: 17053393
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
ID: 17053418
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
ID: 17053430
Yeah sry, was missing a compiler. Take care,
Sebastian
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Events in static methods 3 50
coded character set iptc field within a jpeg 1 69
Two different visual studio versions 3 23
Run time Error 4 34
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

24 Experts available now in Live!

Get 1:1 Help Now