ค้นหาเว็บไซต์

วิธีการเชื่อมต่อสตริงใน PowerShell


หากคุณกำลังทำงานกับสตริงใน PowerShell คุณอาจต้องการเชื่อมสตริงเข้าด้วยกัน ในบทช่วยสอน PowerShell นี้ ฉันจะแสดงวิธีการต่างๆ เพื่อเชื่อมสตริงใน PowerShell เราจะดูวิธีรวมหลายสตริงเป็นสตริงเดียวโดยใช้ PowerShell

หากต้องการเชื่อมสตริงใน PowerShell คุณสามารถใช้ตัวดำเนินการ + ได้ เช่น “John” + ” ” + “Doe” จะให้ผลลัพธ์เป็น “John Doe” อีกวิธีหนึ่งคือการใช้ตัวดำเนินการรูปแบบ -f เช่น “{0} {1}” -f “John”, “Doe” นอกจากนี้ คุณสามารถใช้ Join-String cmdlet สำหรับงานต่อข้อมูลที่ซับซ้อนมากขึ้นได้ นี่เป็นตัวอย่างง่ายๆ:

$firstName = "John"
$lastName = "Doe"
$fullName = $firstName + " " + $lastName
Write-Host $fullName  # Outputs: John Doe

เชื่อมต่อสตริงใน PowerShell

หากต้องการรวมสตริงใน PowerShell เราสามารถใช้วิธีการด้านล่าง:

  • การใช้ตัวดำเนินการ +
  • การใช้ตัวดำเนินการ -f (ตัวดำเนินการรูปแบบ)
  • การใช้การแก้ไขสตริง
  • การใช้ตัวดำเนินการ -join
  • การใช้เมธอด String.Concat()
  • การใช้คลาส StringBuilder

เราจะตรวจสอบวิธีการข้างต้นทั้งหมดพร้อมตัวอย่าง

1. การใช้โอเปอเรเตอร์ +

วิธีที่ง่ายที่สุดในการต่อสตริงใน PowerShell คือการใช้ตัวดำเนินการ + cmdlet นี้เรียบง่ายมากและมีลักษณะเหมือนกับตัวอย่างด้านล่าง

นี่คือสคริปต์ PowerShell ที่สมบูรณ์

$city1 = "New York"
$city2 = "Los Angeles"
$city3 = "Chicago"

$combinedCities = $city1 + ", " + $city2 + ", " + $city3
Write-Output $combinedCities

เอาท์พุต :

New York, Los Angeles, Chicago

คุณสามารถดูผลลัพธ์ได้ในภาพหน้าจอด้านล่างหลังจากที่ฉันรันสคริปต์ PowerShell โดยใช้โค้ด VS

อ่านการต่อสตริงภายในลูปใน PowerShell

2. การใช้ -f Operator (ตัวดำเนินการฟอร์แมต)

โอเปอเรเตอร์ -f ใน PowerShell ช่วยให้จัดรูปแบบสตริงที่ซับซ้อนมากขึ้นได้ คล้ายกับการแก้ไขสตริงในภาษาอื่น

นี่คือสคริปต์ PowerShell ที่สมบูรณ์

$city1 = "Houston"
$city2 = "Phoenix"
$city3 = "Philadelphia"

$combinedCities = "{0}, {1}, {2}" -f $city1, $city2, $city3
Write-Output $combinedCities

เอาท์พุต :

Houston, Phoenix, Philadelphia

คุณสามารถดูผลลัพธ์ได้ในภาพหน้าจอด้านล่าง ฉันรันสคริปต์

3. การใช้การแก้ไขสตริง

การแก้ไขสตริงฝังค่าตัวแปรโดยตรงภายในสตริงใน PowerShell ใน PowerShell คุณสามารถใช้เครื่องหมายคำพูดคู่ (") เพื่อให้บรรลุเป้าหมายนี้ได้

นี่คือสคริปต์ที่สมบูรณ์เพื่อรวมสตริงใน PowerShell

$city1 = "San Antonio"
$city2 = "San Diego"
$city3 = "Dallas"

$combinedCities = "$city1, $city2, $city3"
Write-Output $combinedCities

เอาท์พุต :

San Antonio, San Diego, Dallas

คุณสามารถดูผลลัพธ์ได้ในภาพหน้าจอด้านล่าง ฉันรันสคริปต์ PowerShell

4. การใช้โอเปอเรเตอร์ -join

โอเปอเรเตอร์ -join สามารถใช้เพื่อเชื่อมอาร์เรย์ของสตริงเข้ากับตัวคั่นที่ระบุใน PowerShell สิ่งนี้มีประโยชน์อย่างยิ่งเมื่อต้องจัดการกับรายการ

$cities = @("San Jose", "Austin", "Jacksonville")

$combinedCities = -join($cities, ", ")
Write-Output $combinedCities

เอาท์พุต :

San Jose, Austin, Jacksonville

5. การใช้เมธอด String.Concat()

เมธอด String.Concat จากเฟรมเวิร์ก .NET ยังสามารถใช้เพื่อเชื่อมสตริงใน PowerShell ได้ด้วย

นี่คือสคริปต์ PowerShell ที่สมบูรณ์

$city1 = "San Francisco"
$city2 = "Columbus"
$city3 = "Fort Worth"

$combinedCities = [String]::Concat($city1, ", ", $city2, ", ", $city3)
Write-Output $combinedCities

เอาท์พุต :

San Francisco, Columbus, Fort Worth

คุณสามารถดูผลลัพธ์ได้ในภาพหน้าจอด้านล่าง:

6. การใช้คลาส StringBuilder

หากคุณต้องการเชื่อมต่อสตริงจำนวนมากอย่างมีประสิทธิภาพ คุณสามารถใช้คลาส StringBuilder ใน PowerShell

ด้านล่างนี้เป็นสคริปต์ที่สมบูรณ์สำหรับการต่อสตริงใน PowerShell โดยใช้คลาส StringBuilder

Add-Type -AssemblyName "System.Text"
$stringBuilder = New-Object System.Text.StringBuilder

$cities = @("Indianapolis", "Charlotte", "Seattle")

foreach ($city in $cities) {
    [void]$stringBuilder.Append("$city, ")
}

# Remove the trailing comma and space
if ($stringBuilder.Length -gt 0) {
    $stringBuilder.Length -= 2
}

$combinedCities = $stringBuilder.ToString()
Write-Output $combinedCities

เอาท์พุต :

Indianapolis, Charlotte, Seattle

บทสรุป

ฉันหวังว่าบทช่วยสอนนี้จะช่วยคุณเชื่อม 2 สตริงใน PowerShell ในนั้น ฉันได้อธิบาย 6 วิธีที่แตกต่างกันสำหรับการเชื่อมโยงสตริงใน PowerShell

บทความที่เกี่ยวข้อง