473,320 Members | 1,978 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,320 software developers and data experts.

A string that keeps its length.

Hi,

If I initialize a string like this :

string s = new string(' ', 20);
s = "Hi!";
Console.WriteLine( "["+ s +"]" );

It clearly shows that the declaration of the initial length of the
string serves no purpose for the sake of keeping the size of the
string.

For a task I'm doing I need a string that keeps its length, filled with
spaces on left.
Is there a class already there available ?

Thanks in advance,

Nov 17 '05 #1
4 1324
<cr************@hotmail.com> schrieb
For a task I'm doing I need a string that keeps its length, filled with
spaces on left.
Is there a class already there available ?


Make the string private and use a get and a set-method. In the set-method
just make sure, that the new string will bei length 20.

Flo
Nov 17 '05 #2
<cr************@hotmail.com> a écrit dans le message de news:
11*********************@o13g2000cwo.googlegroups.c om...
If I initialize a string like this :

string s = new string(' ', 20);
This next line replaces the whole string with the string literal "Hi!"
s = "Hi!";
Console.WriteLine( "["+ s +"]" );

It clearly shows that the declaration of the initial length of the
string serves no purpose for the sake of keeping the size of the
string.
You are not replacing characters, you are replacing the whole string. .NET
strings are immutable.
For a task I'm doing I need a string that keeps its length, filled with
spaces on left.
Is there a class already there available ?


I suggest you look at the StringBuilder class.

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #3
<cr************@hotmail.com> wrote:
If I initialize a string like this :

string s = new string(' ', 20);
s = "Hi!";
Console.WriteLine( "["+ s +"]" );

It clearly shows that the declaration of the initial length of the
string serves no purpose for the sake of keeping the size of the
string.
More than that - it serves no purpose whatsoever. You give the variable
s one value, and then immediately give it a different value. You're not
changing the string itself - you're changing which string s refers to.
For a task I'm doing I need a string that keeps its length, filled with
spaces on left.
Is there a class already there available ?


Strings themselves always keep their length, along with all the rest of
the data - they're immutable. However, you could have a *property*
which always preserves the length by keeping a private member variable
which is padded appropriately.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Craig,
In addition to the other comments.

You can use String.PadLeft & String.PadRight to return a new string that is
padded to a certain length.

http://msdn.microsoft.com/library/de...dlefttopic.asp

http://msdn.microsoft.com/library/de...righttopic.asp

| string s = new string(' ', 20);
s = "Hi!".PadRight(20, ' ');
- or -
s = "Hi!".PadRight(20);
| Console.WriteLine( "["+ s +"]" );

Alternatively you could use a format string.

| s = "Hi!";
Console.WriteLine("[{0,-20}]", s);

I would use PadRight in a property, if the backing field always needs to be
a certain length, I would use the a format string, if the length is only
important for "presentation".

Hope this helps
Jay
<cr************@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
| Hi,
|
| If I initialize a string like this :
|
| string s = new string(' ', 20);
| s = "Hi!";
| Console.WriteLine( "["+ s +"]" );
|
| It clearly shows that the declaration of the initial length of the
| string serves no purpose for the sake of keeping the size of the
| string.
|
| For a task I'm doing I need a string that keeps its length, filled with
| spaces on left.
| Is there a class already there available ?
|
| Thanks in advance,
|
Nov 17 '05 #5

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

Similar topics

3
by: ofer h | last post by:
hello all i have got a span inside a td of a table. i am trying to clip the string that exceeds the length of the td and then put three dots ("...") after it so it will show that the text was...
2
by: Tim Simmons | last post by:
I am stumped. I encoded the action = of my form using GET and I can't seem to get the property/value stuff from it using a JavaScript script I got from the web. I want to create a trivia game...
35
by: Felix Kater | last post by:
The C-faq says that "The malloc/free implementation remembers the size of each block allocated and returned, so it is not necessary to remind it of the size when freeing." Could that length...
10
by: cppdev | last post by:
Hi All! I want to clear the string contents from sensitive information such as passwords, and etc. It's always a case that password will appear as string at some point or another. And i feel...
5
by: Ann Marinas | last post by:
Happy New Year to all! :D I am currently developoing an application that imports data from a CSV file. Each comma represents an array item that I need to extract data with. My problem is...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
7
by: junw2000 | last post by:
In the following code: std::string s1; s1 = "ABCDE"; std::cout<<s1<<std::endl; s1 = '\0'; //LINE1 std::cout<<s1<<std::endl; //LINE2 At LINE1, I add a NULL terminator...
13
by: Angus | last post by:
Hello I have a stream of bytes - unsigned char*. But the 'string' may contain embedded nulls. So not like a traditional c string terminated with a null. I need to calculate the length of...
2
by: Peted | last post by:
Hi im sending this string to a device String cmd = ""N1\x0D") this device needs the esc \x0D on the end where you see it After i have sent this string i want to print it our to a rich...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.