Truncating Variables

J

Jason Williard

Is there a built-in function that I can use to truncate variables longer
than 30 characters? Even better, is there a function that truncates and
then appends "(...)" to the end?
 
D

darrel

Is there a built-in function that I can use to truncate variables longer
than 30 characters? Even better, is there a function that truncates and
then appends "(...)" to the end?

Are you asking how to trim a string? If so, you could do this:

dim sourceString as string
dim truncatedString as string
truncatedString = left(sourceString, 30) & "..."

-Darrel
 
J

Jason Williard

Darrel,

This is pretty much what I wanted. However, for the appending of "(...)",
what would it take to do that to strings that are more than 30 characters,
as not all strings are more than 30 chars?

--

Thank You,
Jason Williard
Client Services
PCSafe, Inc.
 
M

Marina

So just check to see if the string is over 30 characters. If it is not,
leave it as is, otherwise perform this transformation on it.
 
G

Greg Burns

I do something like this in SQL (I subract 1 because '...' takes approx 1
character width in my font)

IF LEN(@Description) > @Width
SET @OUT = LEFT(@Description,@Width-1) + '...'
ELSE
SET @OUT = LEFT(@Description,@Width)

In VB you would want to check that your string is not already less than 30
so as not to append "..." to everything.

Greg
 
K

Kevin Spencer

A variable cannot be truncated.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
S

Scott Mitchell [MVP]

Jason said:
Is there a built-in function that I can use to truncate variables longer
than 30 characters? Even better, is there a function that truncates and
then appends "(...)" to the end?

If you are displaying this information in a DataGrid, you might want to
consider checking out this free custom DataGrid column:
http://aspnet.4guysfromrolla.com/articles/100202-1.aspx

It is even a bit smarter - rather than just chopping the sentence off
at, say, 30 characters, is cuts it off only at word boundaries.

Happy Programming!

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
G

Greg Burns

ouch

Kevin Spencer said:
A variable cannot be truncated.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
J

Jason Williard

Thank you for your nice comments. How about a string?

Anyways, the others who have been kind enough to answer my question helped
me solve this.

--

Thank You,
Jason Williard
Client Services
PCSafe, Inc.
 
K

Kevin Spencer

Thank you for your nice comments. How about a string?

As you mentioned, someone had already answered your question.

I know you don't appreciate it, but there is a method to my madness. Knowing
the difference between a variable and a string is essential to being able to
program with .Net. It might seem like semantics to you, but it's serious
business to your computer. You can't get away with that kind of thinking in
..Net. Even VB.Net is nowhere near as forgiving as ASP and VBScript. Many VB
developers are floundering because they don't even know what data types are
(never had to think about it before). Just thank your lucky stars that you
may never have to learn about pointers!

The "point" is, with VBScript and ASP, one didn't have to know much about
the objects one played with. But in .Net, it is essential. This is
real-world programming. Variables, classes, data types, fields, properties,
bits and logic are the building blocks with which you build your app. What
kind of builder will you be if you don't take the time to understand them
first? Sure, I, like everyone else, want to get down and dirty with the
code. But some homework is necessary first. The difference between a
variable and a string is the difference between a suitcase and a change of
clothes. You wouldn't want to show up for work in a suitcase one day, would
you?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
J

Jason Williard

Kevin,

I completely understand where you are coming from. It was a simple mistake
on my part. I know the differences between variables and strings. That
wasn't the issue and going off on a tirade about knowing what a variable is
and is not going to help in these situations. Your comments are really
quite rude and not productive at all. A better response would have been to
answer the question while also reminding me of the differences.
 
K

Kevin Spencer

Someone else had, as I mentioned previously, answered the question.

I think if you look carefully at both of my responses, you will see that
rudeness is purely your perception. And neither answer was a "tirade." That
is pure emotional characterization. My boss pays me good money for this type
of advice. You and everyone else I help every day get it for free.

--
You're welcome,

Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top