How do I make a string longer??

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
Hi,


| 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
 
roger_27 said:
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; ?)
 
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.
 
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.

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
 

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

Back
Top