How do I repeat a character n times in a string?

后端 未结 3 883
难免孤独
难免孤独 2021-01-03 20:54

I am learning Perl, so please bear with me for this noob question.

How do I repeat a character n times in a string?

I want to do something like below:

3条回答
  •  无人及你
    2021-01-03 21:34

    Your regular expression can be written as:

    $numOfChar = 10;
    
    s/^(.*)/(' ' x $numOfChar).$1/e;
    

    but - you can do it with:

    s/^/' ' x $numOfChar/e;
    

    Or without using regexps at all:

    $_ = ( ' ' x $numOfChar ) . $_;
    

提交回复
热议问题