By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,213 Members | 1,517 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,213 IT Pros & Developers. It's quick & easy.

How do I make a string longer??

P: n/a
hey, I have a bunch of strings that are 20 characters long. The user inputs
data 10 characters long and I am inserting data at character spot number 11.

well what if the user inputs data that is 5 characters long? how do I insert
spaces untill it is 10 characters long, so I can still use the string.insert
at space number 11?

I hope this made sense. thanks.
Jan 25 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
JR
Pad

JR
"roger_27" <ro*****@discussions.microsoft.comwrote in message
news:A5**********************************@microsof t.com...
hey, I have a bunch of strings that are 20 characters long. The user
inputs
data 10 characters long and I am inserting data at character spot number
11.

well what if the user inputs data that is 5 characters long? how do I
insert
spaces untill it is 10 characters long, so I can still use the
string.insert
at space number 11?

I hope this made sense. thanks.

Jan 25 '07 #2

P: n/a
Hi,

Use PadLeft or PadRight. Or better still...what about StringBuilder?

Brian

On Jan 25, 12:27 pm, roger_27 <roge...@discussions.microsoft.com>
wrote:
hey, I have a bunch of strings that are 20 characters long. The user inputs
data 10 characters long and I am inserting data at character spot number 11.

well what if the user inputs data that is 5 characters long? how do I insert
spaces untill it is 10 characters long, so I can still use the string.insert
at space number 11?

I hope this made sense. thanks.
Jan 25 '07 #3

P: n/a
Hi,
"roger_27" <ro*****@discussions.microsoft.comwrote in message
news:A5**********************************@microsof t.com...
| hey, I have a bunch of strings that are 20 characters long. The user
inputs
| data 10 characters long and I am inserting data at character spot number
11.
|
| well what if the user inputs data that is 5 characters long? how do I
insert
| spaces untill it is 10 characters long, so I can still use the
string.insert
| at space number 11?

Remember that String is an inmutable class, this means that if you try to
modify it in any way you are creating a new instance.

With that remark in place you can use Pad or simply adding a new string like

corrected = user_input + new String( ' ', 10 - user_input);
Take also a look at StringBuilder class

--
Ignacio Machin
machin AT laceupsolutions com
Jan 25 '07 #4

P: n/a
roger_27 <ro*****@discussions.microsoft.comwrote:
hey, I have a bunch of strings that are 20 characters long. The user inputs
data 10 characters long and I am inserting data at character spot number 11.

well what if the user inputs data that is 5 characters long? how do I insert
spaces untill it is 10 characters long, so I can still use the string.insert
at space number 11?

I hope this made sense. thanks.
string padded = originalString.PadRight(10);

(If you've always got an original string which is 10 characters long
though, why are you using Insert to append to the end of it? Why not
just use string extra = original + otherBit; ?)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 25 '07 #5

P: n/a
because Im replacing this same string many times... there is quite a crazy
situation... basically i take a huge string, and pass pieces of the string 17
characters in length to a printer. also, add text to the end of it that says
"Line 1" "Line 2" "Line 3"

but what if the last line is less than 17 characters? I cant add the line
number just to the end,. it wont be right justified with the rest of the line
numbers. so I need to add spaces till its long enough,.

its actually more complicated than that. but I'm just showing you a perfect
example of where I need to do this.

"Jon Skeet [C# MVP]" wrote:
roger_27 <ro*****@discussions.microsoft.comwrote:
hey, I have a bunch of strings that are 20 characters long. The user inputs
data 10 characters long and I am inserting data at character spot number 11.

well what if the user inputs data that is 5 characters long? how do I insert
spaces untill it is 10 characters long, so I can still use the string.insert
at space number 11?

I hope this made sense. thanks.

string padded = originalString.PadRight(10);

(If you've always got an original string which is 10 characters long
though, why are you using Insert to append to the end of it? Why not
just use string extra = original + otherBit; ?)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 25 '07 #6

P: n/a
On Thu, 25 Jan 2007 14:35:01 -0800, roger_27 <ro*****@discussions.microsoft.com>
wrote:
>because Im replacing this same string many times... there is quite a crazy
situation... basically i take a huge string, and pass pieces of the string 17
characters in length to a printer. also, add text to the end of it that says
"Line 1" "Line 2" "Line 3"

but what if the last line is less than 17 characters? I cant add the line
number just to the end,. it wont be right justified with the rest of the line
numbers. so I need to add spaces till its long enough,.

its actually more complicated than that. but I'm just showing you a perfect
example of where I need to do this.

"Jon Skeet [C# MVP]" wrote:
>roger_27 <ro*****@discussions.microsoft.comwrote:
hey, I have a bunch of strings that are 20 characters long. The user inputs
data 10 characters long and I am inserting data at character spot number 11.

well what if the user inputs data that is 5 characters long? how do I insert
spaces untill it is 10 characters long, so I can still use the string.insert
at space number 11?

I hope this made sense. thanks.

string padded = originalString.PadRight(10);

(If you've always got an original string which is 10 characters long
though, why are you using Insert to append to the end of it? Why not
just use string extra = original + otherBit; ?)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
string test = "test";

string output = string.Format("{0,-17}{1}", test, "Line 1");

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Jan 26 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.