String question

  • Thread starter Thread starter ma
  • Start date Start date
M

ma

Hello,
I want to create a string with the exact size of say X that each char be
'.'. How can I do this? I tried the following and it did not work:

int x=10;
string tmp="";
tmp.padright(x,'.');

Regards
 
Strings are immutable, meaning, once you have an instance of it, you
can't change the contents (you actually can, but you have to bend over
backwards to do it).

Rather, all the methods on the string class which appear to modify it
actually return a new instance, so you would do this:

int x = 10;
string tmp = "";
tmp = tmp.PadRight(x, '.');

Of course, in this instance, it would be easier to construct the string
with the characters from the get-go:

int x = 10;
string tmp = new string('.', 10);
 
string is immutable, so the new value is always the /result/ of the
call - i.e.
string padded = tmp.PadRight('.',x);

Alternatively, if you know the original string is empty:
string padded = new string('.',x);

Marc
 
Hi,

ma said:
Hello,
I want to create a string with the exact size of say X that each char be
'.'. How can I do this? I tried the following and it did not work:

One of the constructors do just that:

string s = new String( '.', 10);
 

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