473,396 Members | 2,013 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,396 software developers and data experts.

Truncate String

The code below is suppose to work were if a user enters a value in the
text box 12 characters long it will hit this loop and truncate the
value to only 12 characters long....however this doesn;t happen...what
do i need to do?

If txtRightLabel.Text.Substring(0, 12) Then
Dim Righttextlable As String
Righttextlable = txtRightLabel.Text
txtRightLabel.Text =
Righttextlable.Substring(Righttextlable.Length - 12)
End If
Oct 9 '08 #1
2 9592
cmdolcet69 wrote:
The code below is suppose to work were if a user enters a value in the
text box 12 characters long it will hit this loop and truncate the
value to only 12 characters long....however this doesn;t happen...what
do i need to do?

If txtRightLabel.Text.Substring(0, 12) Then
Two errors:

1. The expression returns a string, which you use as a condition in the
If statement.

2. If the text is shorter than 12 characters you will get an exception
when you try to get the first 12 characters.
Dim Righttextlable As String
Righttextlable = txtRightLabel.Text
txtRightLabel.Text =
Righttextlable.Substring(Righttextlable.Length - 12)
This would not make the string 12 characters long, it would shorten the
string by 12 characters.
End If
From your specification, this is what you want:

If txtRightLabel.Text.Length 12 Then
txtRightLabel.Text = txtRightLabel.Text.Substring(0, 12)
End If

or:

txtRightLabel.Text = txtRightLabel.Text.Substring(0,
Math.Min(txtRightLabel.Text.Length, 12))

Or why not simply limit the text box to 12 characters?

txtRightLabel.MaxLength = 12

--
Göran Andersson
_____
http://www.guffa.com
Oct 9 '08 #2
Göran Andersson wrote:
or:

txtRightLabel.Text = txtRightLabel.Text.Substring(0,
Math.Min(txtRightLabel.Text.Length, 12))
or:

txtRightLabel.Text = Strings.Left(txtRightLabel.Text, 12)

--

(O)enone
Oct 9 '08 #3

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

Similar topics

4
by: elyob | last post by:
How can I get -0.162058 to -0.1 rather than rounding to -0.2? I know I'm being stupid here, but it's really bugging me. The number before the decimal point could 0 - 999 Thanks
1
by: Yannick Turgeon | last post by:
Hello all, I just started to use perl and I'm having a Regexp question: Say we've got a file with this in it: ---- Beginning of file after this line -------- My list A: cat B: dog C: horse
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...
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...
9
by: Sumanth | last post by:
Are there any implementations of truncate in db2. Is it going to be implemented in the future? Is there an alternate way of doing a truncate of a table that has a high record count without using...
2
by: Stimp | last post by:
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...
10
by: Troels Arvin | last post by:
Hello, Until this date, I believed that DB2 has no TRUNCATE TABLE command. But then I came across...
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...
5
by: Timothy Madden | last post by:
Hello I see there is now why to truncate a file (in C or C++) and that I have to use platform-specific functions for truncating files. Anyone knows why ? I mean C/C++ evolved over many years...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.