Solved

Trimming a string to shorten at an unspecified position.

Posted on 2006-07-06
7
264 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
System.Speech 2 26
WCF Service Application cannot connect from TCP terminal 1 31
Google Directions API to Map URL -C#? 3 22
Where did System.Data.Objects go? 2 23
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

774 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