473,412 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

truncate string to last full stop (within 160 chars)

Hi all,

Having a bit of a headache trying to do this.

I want to create a string with a total length of 160 characters.

The first few characters must always be in the string.. say around 45
characters.

After this I want to append another string, description, which can be
any number of characters long.

I want to truncate this string so that when it is appended onto the
other string it doesn't exceed 160 characters BUT I want it truncated
to the nearest '.'.

I've been messing around with substring but not much luck... anyone done
this before?

Cheers,
Peter
--

fiddlewidawiddum
Oct 27 '06 #1
2 2276
I'm sure someone here can codesmith this a bit better for optimization, but
perhaps this will lead you on the right path.

I left the var names a bit more telling...

string firstString = "Product Name Is Here And is Around 45 Characters
Long.";
int firstStringLength = firstString.Length;

string secondString
= "This is a long description. This long description is really
long. " +
"Boy this is a really long description. Is anyone still reading
this really long description. " +
"Hello out there, are you stil reading? Hello?";

string cutoffString = secondString.Substring(0, 159 - firstStringLength);

int lengthtoLastPeriod = cutoffString.LastIndexOf(".") + 1;

string newString = firstString + " " + cutoffString.Substring(0,
lengthtoLastPeriod);

On the cutoffString, I read your post as if the TOTAL of string1 and string2
could not exceed 160 characters. If that's not the case, remove the "- firstStringLength"
part. :)

Hope this helps!

-dl

--
David Longnecker
Web Developer
http://blog.tiredstudent.com
Hi all,

Having a bit of a headache trying to do this.

I want to create a string with a total length of 160 characters.

The first few characters must always be in the string.. say around 45
characters.

After this I want to append another string, description, which can be
any number of characters long.

I want to truncate this string so that when it is appended onto the
other string it doesn't exceed 160 characters BUT I want it truncated
to the nearest '.'.

I've been messing around with substring but not much luck... anyone
done this before?

Cheers,
Peter

Oct 27 '06 #2
On Fri, 27 Oct 2006 Stimp <re*@spumco.comwrote:
Hi all,

Having a bit of a headache trying to do this.

I want to create a string with a total length of 160 characters.

The first few characters must always be in the string.. say around 45
characters.

After this I want to append another string, description, which can be
any number of characters long.

I want to truncate this string so that when it is appended onto the
other string it doesn't exceed 160 characters BUT I want it truncated
to the nearest '.'.

I've been messing around with substring but not much luck... anyone done
this before?

Cheers,
Peter
I got it in the end...

Dim iCharactersAvailable As Integer = 160 - iTotalTextChars

sDescription = sDescription.Substring(0, sDescription.LastIndexOf(".",
iCharactersAvailable, iCharactersAvailable) + 1)
--

fiddlewidawiddum
Oct 27 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: New MSSQL DBA | last post by:
I have recently been assigned to take over several MSSQL environments and found some of the existing practice confusing. As most of my previous experiences are on Oracle and Unix platform so would...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
9
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how...
3
by: John A Grandy | last post by:
i have written code to do the following task ... * truncate a string to the nearest word so that the result string is less than a certain length .... but i am wondering the if .net class lib...
5
by: ronin 47th | last post by:
Hi group, In one of the books 'Gurus Guide to Transact SQL' i found this info: ------------------------------------------------------------ TRUNCATE TABLE empties a table without logging row...
10
by: klineb | last post by:
Good Day, I have written and utility to convert our DOS COBOL data files to a SQL Server database. Part of the process requires parsing each line into a sql statement and validting the data to...
5
by: magix | last post by:
If I have this strings in my file: 0044541CR97274 3 7 20060824060624 0034541CR97275 4 7 20060826060624 0054544CR97276 5 7 ...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
13
by: Hongyu | last post by:
Hi, I have a datetime char string returned from ctime_r, and it is in the format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars including the last terminate char '\0', and i would...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.