Powershell:- Repeat Character

Background

A few weeks ago, I was in a jam.

I wanted to draw a line on my console to separate out two sections of output.

It seems terrible inefficient to use Write-Output(“—————————————-“).

I preferred something like Write-Output(repeatChars(‘-‘, 80)).

Code

Here is stolen code that I have to come back and properly attribute ( give credit ).



function repeatChars([char] $ch, [int] $length)
{
	
	[string] $data = "";
	
	<#
		Initiate new Object
		
			Object Type:- String
			
			Argument List:- 
			
				a) Char ( $ch )
				
				b) Number of characters ( $length)
				
	#>
	$data = New-Object -TypeName String -Argument List $ch, $length;	

	return ( $data );
	
}

Basis

Microsoft

.Net

Learn > .NET > API browser > System > String > String Constructors

Link

Overloaded constructor syntax
Repeat Char (c), N Times


String(Char c, Int32 count)

Initializes the new instance to the value indicated by a specified Unicode character repeated a specified number of times.

References

  1. Microsoft
    • .Net
      • Learn > .NET > API browser > System > String
        • String Constructors
          Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s