我正在從 csv 檔案中匯入一些值,并使用它們為帶有以下代碼的 Android 意圖創建 adb 命令。
Write-Host adb shell am start -a android.intent.action.VIEW '-d' '"https://api.whatsapp.com/send?phone=' $($c.number)'"'
這給了我一個輸出:
adb shell am start -a android.intent.action.VIEW -d "https://api.whatsapp.com/send?phone= 12345678 "
如何洗掉變數連接到字串的空格以提供以下輸出:
adb shell am start -a android.intent.action.VIEW -d "https://api.whatsapp.com/send?phone=12345678"
uj5u.com熱心網友回復:
通過切換到雙引號使用字串插值:
Write-Host adb shell am start -a android.intent.action.VIEW '-d' "`"https://api.whatsapp.com/send?phone=$($c.number)`""
在雙引號內,您必須反引號轉義雙引號才能按字面意思輸出它們。
uj5u.com熱心網友回復:
zett42 的有用答案無疑是您問題的最佳解決方案。
至于你嘗試了什么:
Write-Host ... '"https://api.whatsapp.com/send?phone=' $($c.number)'"'
前面有一個空格
$($c.number)的事實意味著您至少傳遞了兩個引數。但是,由于 PowerShell 的引數模式決議怪癖,您傳遞了三個,因為
'"'直接跟隨的字串$($c.number)也成為它自己的引數。- 有關更多資訊,請參閱此答案。
因此,在 PowerShell 中最好避免使用復合字串引數(由帶引號和不帶引號/不同引號的標記的混合組成)。
所以:
要么:使用單個可擴展(雙引號)字串 (
"..."),如 zett42 的答案。或者:使用包含在中的運算式
(...),并使用運算子與字串連接,如下所示。
Write-Host ... ('"https://api.whatsapp.com/send?phone=' $c.number '"')
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/521969.html
標籤:电源外壳
