I'd like to write a procedure in Powershell, which takes a string as an input and moves…
- User
I'd like to write a procedure in Powershell, which takes a string as an input and moves all characters between the first occurance of "(" and the first occurance of ")" to the beginning of that string and then returns that new string as an output. Can you help me?
Assistant
Here's a PowerShell function that does exactly what you need:
function Move-ParenthesisContent { param ( [string]$InputString ) $openIndex = $InputString.IndexOf("(") $closeIndex = $InputString.IndexOf(")") if ($openIndex -ne -1 -and $closeIndex -ne -1 -and $closeIndex -gt $openIndex) { $contentInParentheses = $InputString.Substring($openIndex + 1, $closeIndex - $openIndex - 1) $beforeParentheses = $InputString.Substring(0, $openIndex) $afterParentheses = $InputString.Substring($closeIndex + 1) return $contentInParentheses + $beforeParentheses + "(" + ")" + $afterParentheses } # Return original string if no valid parentheses found return $InputString }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.