How to terminate variable expansion to create a st

2020-08-10 19:42发布

问题:

How do I tell powershell the end of a variable to be expanded into a string when it sits next to other alphabetic characters?

$StringToAdd = "iss"
$CompeteString = "Miss$StringToAddippi"

Thanks!

回答1:

Use curly braces, { and }, to delimit the variable expansion. For example:

PS C:\> $StringToAdd = "iss"
PS C:\> $CompeteString = "Miss${StringToAdd}ippi"
PS C:\> $CompeteString
Mississippi


回答2:

You can use $()

PS C:\> $StringToAdd = "iss"
PS C:\> $CompeteString = "Miss$($StringToAdd)ippi"
PS C:\> $CompeteString
Mississippi

The sub-expression operator for double-quoted strings is described here. Whatever is in the brackets should be evaluated first. This can be a variable or even an expression.

PS C:\> $CompeteString = "Miss$($StringToAdd.length * 2)ippi"
PS C:\> $CompeteString
Miss6ippi


回答3:

$CompleteString="Miss"+$StringToAdd+"ippi"



标签: powershell