Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Is there a simple way of filling a string with a special caracter ("X" for example) in C# ? I'm looking for something as easy as StrDup in VB.

 

 

Thanks !

Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone!

--Homer Simpson

Posted (edited)

Here is a solution

 

This will do the trick.

 

string str = "";
str.PadRight( <TOTAL WIDTH>, [YOUR CHAR]);
str.PadLeft( <TOTAL WIDTH>, [YOUR CHAR]);

 

it will add them from left or from right.

 

1st parameter is the total number of character... so if you have less than the number... all the other will be filled with your character.

 

The default implementation (with 1 parameter) will pad with space.

 

To add a certain number of character... consider this function :

 

public string AddToRight(string src, char c, int num)
{
 string tmp = "";
 tmp = tmp.PadRight( num, c);

 return src + tmp;
}

Edited by Arch4ngel

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Posted
Cool, that does the trick, just as you said. I still have a lot to learn in C#, syntax is not quite like VB.NET ;-)

Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone!

--Homer Simpson

Posted
Is there a simple way of filling a string with a special caracter ("X" for example) in C# ? I'm looking for something as easy as StrDup in VB.

 

 

string str = new string( 'X', 5); // result = "XXXXX"

 

Bye!

Posted
Well ... this could replace the padding in my function :p lol

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...