473,480 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Truncating a string...

VM
How can I truncate a string? For example, if I have a string containing
"Hello All" but the space reserved for this string is of 4 chars, how can I
truncate it to "Hell"? Would I need to create my own method for this or is
there an exisiting String method that does it?
Thanks

Nov 16 '05 #1
5 37537
yourStringInstance.Substring will do it.

--
John Wood
EMail: first name, dot, second name at priorganize.com
"VM" <vo******@yahoo.com> wrote in message
news:eh**************@TK2MSFTNGP12.phx.gbl...
How can I truncate a string? For example, if I have a string containing
"Hello All" but the space reserved for this string is of 4 chars, how can I truncate it to "Hell"? Would I need to create my own method for this or is
there an exisiting String method that does it?
Thanks

Nov 16 '05 #2
Try this,

string myStr = "Hello All"

//remove characters after 4.
myStr = myStr.Remove(4,myStr.Length-4);

--
Shak
(Houston)


"VM" <vo******@yahoo.com> wrote in message
news:eh**************@TK2MSFTNGP12.phx.gbl...
How can I truncate a string? For example, if I have a string containing
"Hello All" but the space reserved for this string is of 4 chars, how can I truncate it to "Hell"? Would I need to create my own method for this or is
there an exisiting String method that does it?
Thanks

Nov 16 '05 #3
VM
But string.SubString assumes that the string size I'll be extracting is less
than/equal to the string source. If I extract a substring of size 12 from a
string of 4 chars, it'll throw a runtime exception.
"John Wood" <sp**@isannoying.com> wrote in message
news:#l**************@TK2MSFTNGP10.phx.gbl...
yourStringInstance.Substring will do it.

--
John Wood
EMail: first name, dot, second name at priorganize.com
"VM" <vo******@yahoo.com> wrote in message
news:eh**************@TK2MSFTNGP12.phx.gbl...
How can I truncate a string? For example, if I have a string containing
"Hello All" but the space reserved for this string is of 4 chars, how can
I
truncate it to "Hell"? Would I need to create my own method for this or

is there an exisiting String method that does it?
Thanks


Nov 16 '05 #4
VM
With string.Remove I also have to check the size of the string... It's more
or less like the substring.

"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:uy**************@TK2MSFTNGP09.phx.gbl...
Try this,

string myStr = "Hello All"

//remove characters after 4.
myStr = myStr.Remove(4,myStr.Length-4);

--
Shak
(Houston)


"VM" <vo******@yahoo.com> wrote in message
news:eh**************@TK2MSFTNGP12.phx.gbl...
How can I truncate a string? For example, if I have a string containing
"Hello All" but the space reserved for this string is of 4 chars, how can
I
truncate it to "Hell"? Would I need to create my own method for this or

is there an exisiting String method that does it?
Thanks


Nov 16 '05 #5
That's correct, it requires two operations if you expect that the string may
be less than the truncated size request:

x.Substring(0, Math.Min(x.Length, size))

--
John Wood
EMail: first name, dot, last name, at priorganize.com
"VM" <vo******@yahoo.com> wrote in message
news:uL**************@TK2MSFTNGP12.phx.gbl...
But string.SubString assumes that the string size I'll be extracting is less than/equal to the string source. If I extract a substring of size 12 from a string of 4 chars, it'll throw a runtime exception.
"John Wood" <sp**@isannoying.com> wrote in message
news:#l**************@TK2MSFTNGP10.phx.gbl...
yourStringInstance.Substring will do it.

--
John Wood
EMail: first name, dot, second name at priorganize.com
"VM" <vo******@yahoo.com> wrote in message
news:eh**************@TK2MSFTNGP12.phx.gbl...
How can I truncate a string? For example, if I have a string containing "Hello All" but the space reserved for this string is of 4 chars, how can
I
truncate it to "Hell"? Would I need to create my own method for this
or is there an exisiting String method that does it?
Thanks



Nov 16 '05 #6

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

Similar topics

4
21929
by: qazmlp | last post by:
Can anybody comment(& suggest improvements) on the following implementation that is for truncating the character buffer contents to the desired length? Truncating char array void...
5
5413
by: VISHNU VARDHAN REDDY UNDYALA | last post by:
Hello, Can someone over here help me in truncating a float variable. I mean if PI=3.14159 ...How can I get to read the first two or first three decimal values with out rounding them. Any...
2
5062
by: Cole Shelton | last post by:
Hi all, I am simply trying to do a transform to a base 64 string and back, but for some reason, the FromBase64Transform is return less bytes than it should. I have verified that the base64...
1
2506
by: Jitesh Sinha | last post by:
Hi, I am running Windows 2003/ IIS 6.0. I was stuck with rather a abnormal behaviour of System.Web.mail class. It was truncating the message body after 3,071 character. The code i was testing...
3
2414
by: Ron | last post by:
I *just* know there's a function to do this, but I can't find it: I want to take the following string: c:\winnt\23342432sss\2343243ccc\32432432423eeee\2432424cc\t...
4
3369
by: DotNetJunkies User | last post by:
I am calling a VB6 dll from a vb.net windows application that returns an array of strings. My issue is it seems to truncate after a NULL character. For Example VB 6 is returning a string with the...
3
2240
by: rangermccoy | last post by:
Hello there, What are the best php/c libraries for handling media including images, video, and music? I would like to manipulate media dfiles, including watermarking, thumbnailing,...
1
1628
by: Daniel Wilson | last post by:
We have a stored procedure that is giving us an XML representation (using FOR XML Explicit) of a sales order. Sometimes that evaluats to quite a long string. Unfortunately, this XML string is...
15
5177
geolemon
by: geolemon | last post by:
I'm having a seriously Twilight Zone moment: In the code below, I'm building a SQL command into a string variable, declared at the top of my code. Then, I connect to the database and try to...
0
7039
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
7037
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
7080
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
6895
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...
1
4770
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
4476
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...
0
2992
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
176
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.