string builder

문자열 뒤집기
문자열 뒤집기 PowerShell에서
function ReverseString([string] $String) {
    $strBuilder = [System.Text.StringBuilder]::new()
    foreach ($i in ($String.Length - 1)..0) {
        [void] $strBuilder.Append($String[$i])
    }
    $strBuilder.ToString()
}

First we create a new StringBuilder object from .NET. Then we loop from the last index of the string to 0, each pass we append the current char to the StringBuilder object. The append method has it own output showing some metadata, to avoid this we can ether cast [void] before the expression or pipe the output into Out-Null to suppress it. When the loop is finished, we called the ToString() method on the StringBuilder object.

StringBuilder class.

Out-Null cmdlet.

ToString method.

2026년 09월 23일 · 유용했나요?