locked
Repeat string RRS feed

  • Question

  • User-172676812 posted

    Hello,

    I would like equivalent in C# from function php : str_repeat ("myString", "int number repeat");

    Thanks

    Monday, March 20, 2006 8:46 AM

All replies

  • User-2041805088 posted

    In C#, you can use a for loop.

    for (int i=0; i<5; i++)
    {
       // Code here
    }

    HTH,
    Ryan

    Monday, March 20, 2006 9:16 AM
  • User545182966 posted

    To repeat a "char" you can use in C#:

       Int32 intNumRepeat = 10;
       Char charToRepeat = 'x';
       String str = new String(charToRepeat, intNumRepeat );

    ;-)

    Victor Rafael Ramaldes Avalos

     

     

    Wednesday, October 25, 2006 6:02 PM
  • User-344571509 posted

    I managed to get it in one line.. ;)

    string somestring = new String(' ', intNumberToRepeat).Replace(" ", "myString");<o:p></o:p>

    Regards,
    Michael Shorten
    MB Limited LLC.
     

    Wednesday, January 24, 2007 4:27 PM
  • User1873786028 posted

    Or you could add it to the left of your string:

    somestring.Text.PadLeft(intNumberToRepeat, 'myString')

     

    Or Right:

    somestring.Text.PadRight(intNumberToRepeat, 'myString')

     

    Friday, January 26, 2007 4:28 PM
  • User1049039376 posted

    1) you can't pad with a string, only with characters

    2) this would be a more efficient one line solution: new System.Text.StringBuilder().Insert(0,"myString",count).ToString()

    Thursday, March 27, 2008 3:34 PM