简体   繁体   中英

Powershell, function add trailing space while concatenating string (parameters)

here's my issue :

function Dummy ($path1, $fileName1) {
    Write-Host "$path1$fileName1"
}

Dummy 'folder\', 'test2.exe'

result is :

folder\ test2.exe

I have just no idea why this whitespace is added. I tried different formatting option (trim, string formatting, even native .net one without result).

Regex kind of work as a "post issue" but is too extrem fo my usage (folder with whitespace in name)

Note that those option i tried work perfectly (or should i say "as expected" -_-') OUT of the function.

if anyone could tell me how dumb i am i'd be glad -_-'

NB : i keep adding a simple "hi" first line of this thread, it just get removed everytime so, sorry for this.

It is because you are not sending in two parameters even though it might look that way. When you use a comma you are actually sending in an array. So $path will be an array with the values 'folder\\' and 'test2.exe' . And of course $fileName1 will be empty.

So when you convert $path to a string the values will be separated by a space.

Remove the comma from the call and it will work:

Dummy 'folder\' 'test2.exe'

As a sidenote: I'm not quite sure what you are trying to do. But if you want to create paths I would recommend using:

Join-Path "folder" "test2.exe"

You have to do this:

function Dummy ($path1, $fileName1) {
    Write-Host "$($path1)$($fileName1)"
}

Dummy 'folder\', 'test2.exe'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM