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

PowerShell เชื่อมต่อสตริง: ตัวอย่างและฟังก์ชัน | การศึกษา


ข้อมูลเบื้องต้นเกี่ยวกับสตริงการต่อข้อมูล PowerShell

การต่อสตริงของ PowerShell เป็นกระบวนการของการรวมสตริงตั้งแต่หนึ่งสตริงขึ้นไป การต่อสตริงสามารถทำได้โดยใช้ตัวดำเนินการ "+" เป็นหลัก มีวิธีอื่นๆ เช่น การล้อมสตริงไว้ในเครื่องหมายคำพูดคู่ การใช้ตัวดำเนินการรวม หรือใช้ตัวดำเนินการ -f

สารบัญ
  • การแนะนำ
  • คุณสมบัติ
  • ตัวอย่าง
  • ฟังก์ชันสตริงอื่น ๆ ที่ใช้กันทั่วไป
  • บทสรุป
  • คำถามที่พบบ่อย

ไวยากรณ์:

ตัวดำเนินการ '+' ใช้เพื่อเชื่อมสตริงเข้าด้วยกัน มาดูตัวอย่างด้านล่าง:

รหัส:

Write-Host "String Concat in Powershell"
Write-Host "Basic Example"
$str1="My name is John."
$str2="Am from Africa"
Write-Host "Concatenated text is below"
$str1+$str2

เอาต์พุต:

จุดเด่นที่สำคัญ

  • PowerShell เป็นซอฟต์แวร์อิสระที่ปรับแต่งได้ ซึ่งผู้จำหน่ายและนักพัฒนาระดับองค์กรสามารถปรับแต่งเครื่องมือและยูทิลิตี้ของตนได้
  • การต่อข้อมูลเป็นวิธีการรวมสองสายโดยใช้ตัวดำเนินการบางตัว
  • การต่อสตริงของ PowerShell เกิดขึ้นกับตัวสร้างสตริง สตริงที่มีตัวเลข ฟังก์ชันในตัว และตัวดำเนินการ

คุณสมบัติของ PowerShell

คุณสมบัติหลักและคำอธิบายสำหรับ PowerShell:

  • Get-Command สร้างรายการคำสั่งทั้งหมดจาก cmdlets และฟังก์ชันของอุปกรณ์ Cmdlet จัดการรีจิสทรี กระบวนการ บริการ บันทึกเหตุการณ์ และฟังก์ชันการดูแลระบบอื่นๆ โดยใช้ Windows Management Instrumentation (WMI) เพื่อทำหน้าที่เหล่านี้ทั้งหมด
  • get-Help cmdlet ช่วยให้ผู้ใช้เรียนรู้เพิ่มเติมเกี่ยวกับหลักการของ PowerShell และส่วนประกอบเฉพาะ
  • การใช้ Windows Management Instrumentation และ WS-Management ผู้ดูแลระบบสามารถควบคุมอุปกรณ์จากระยะไกลได้
  • ด้วยตัวดำเนินการไปป์ PowerShell (|) คุณสามารถเชื่อมโยงคำสั่งเข้าด้วยกันได้ ในแนวทางนี้ เอาต์พุตของคำสั่งเดียวคืออินพุตสำหรับคำสั่งที่กำลังจะเกิดขึ้นในลักษณะไปป์ไลน์ ตัวดำเนินการไปป์ไลน์ PowerShell อนุญาตให้มีการไหลของวัตถุแทนที่จะเป็นสตริงข้อความจาก cmdlet ที่หนึ่งไปยังอีกที่หนึ่ง
  • ภาษาสคริปต์ PowerShell ให้การสนับสนุนสคริปต์และเครื่องมือบรรทัดคำสั่งที่มีอยู่แล้ว
  • cmdlet และที่เก็บข้อมูลระบบมีรูปแบบการตั้งชื่อที่เหมือนกัน และใช้ไวยากรณ์ทั่วไปที่ทำให้การแบ่งปันข้อมูลเป็นเรื่องง่าย
  • การนำทางตามคำสั่งในลักษณะที่เรียบง่ายทำให้ผู้ใช้สามารถนำทางไปยังรีจิสทรีและที่เก็บข้อมูลอื่น ๆ ได้
  • PowerShell มีความสามารถในการจัดการวัตถุที่มีประสิทธิภาพ สามารถส่งออบเจ็กต์ไปยังเครื่องมือหรือฐานข้อมูลอื่นได้โดยตรง

ตัวอย่างของสตริงที่ต่อกันของ PowerShell

ไม่จำเป็นว่าต้องใช้เฉพาะเครื่องหมาย '+' เพื่อเชื่อมสตริงเข้าด้วยกัน ตัวอย่างนี้จะแสดงวิธีต่างๆ ที่สามารถต่อสตริงเข้าด้วยกันได้

ตัวอย่างที่ 1 - มีตัวคั่น

รหัส:

Write-Host "String concatenation without + operator"
$str1="first sentence"
$str2="second sentence"
Write-Host "After concatenation"
"$str1$str2"
Write-Host "Example of concatenation with separators"
$str1+ "," +$str2
$str3="Powershell is the most underrated language"
$str4=" it can do many wonders"
Write-Host "Another way"
"$str3!!!!,$str4"

เอาต์พุต:

ตัวอย่างที่ 2 - การต่อสตริงและจำนวนเต็ม

รหัส:

Write-Host "String concatenation demo of int and string"
$string1=75
$string2="my name is Alex"
$string3=$string1 + $string2

เอาต์พุต:

ในโค้ดข้างต้น หากตัวแปรถูกสลับก่อนการต่อข้อมูล ข้อผิดพลาดจะไม่เกิดขึ้น

อินพุต:

Write-Host "String concatenation demo of int and string"
$string1=75
$string2="my name is Alex"
$string3=$string2 + $string1
Write-Host $string3

เอาต์พุต:

อีกวิธีหนึ่ง สามารถทำได้โดยใช้การทดแทนสตริง

รหัส:

Write-Host "String concatenation demo of int and string"
$int1=75
$string2="my name is Alex"
Write-Host "string concatenation using string Substitution"
"$($int1) , $($string2)"

เอาต์พุต:

ตัวอย่าง #3 - การใช้ -f โอเปอเรเตอร์

รหัส:

Write-Host "String concatenation using f operator"
$str1="This is the first sentence"
$str2="This is the second sentence"
$str3="This is the third Sentence"
Write-Host "Concatenated string is below"
"{0}.{1}.{2}." -f $str1,$str2,$str3

เอาต์พุต:

ตัวอย่าง #4 - การใช้ตัวดำเนินการเข้าร่วม

รหัส:

Write-Host "String concatenation using Join operator"
$str1="This is the first sentence"
$str2="This is the second sentence"
$str3="This is the third Sentence"
Write-Host "Concatenated string is below"
$str1,$str2,$str3 -join "!!!"

เอาต์พุต:

ตัวอย่าง #5 - ลบพื้นที่เริ่มต้นใน Write-Host

รหัส:

$str1="This is the first sentence"
$str2="This is the second sentence"
$str3="This is the third Sentence"
Write-Host "Concatenated string is below" $str1 $str2 $str3 -Separator ''

เอาต์พุต:

ตัวอย่าง #6 - การใช้ Concat() และ String Builder

รหัส:

Write-Host "String concatenation using concat method"
$str1="This is the first sentence"
$str2="This is the second sentence"
$str3="This is the third Sentence"
Write-Host "Concatenated string is below"
[System.String]::Concat($str1,".",$str2,".",$str3)
Write-Host "String concatenation using string builder"
$SB = New-Object -TypeName System.Text.StringBuilder
$null = $SB.Append("First Sentence")
$null = $SB.Append("Second Sentence")
$null = $SB.Append("Third Sentence")
$null = $SB.Append("Fourth Sentence")
$SB.ToString()

เอาต์พุต:

ตัวอย่าง #7

รหัส:

$string1 = @"
My name is Ella
Am from USA
Am a freelancer
"@
$string2 = @"
First sentence of the example
Second sentence of the example
Third Sentence of the example
"@
$string1 , $string2 -join "`n"

เอาต์พุต:

ตัวอย่าง #8

เชื่อมต่อค่าสองคอลัมน์เข้าด้วยกันใน CSV และส่งออกไปยังคอลัมน์ใหม่

ในตัวอย่างด้านล่าง CSV มีสองคอลัมน์ ได้แก่ ชื่อและนามสกุล สคริปต์ด้านล่างนี้จะรวมค่าของคอลัมน์ทั้งสองเข้าด้วยกันและส่งออกไปยังคอลัมน์ใหม่ที่เรียกว่า FullName

เนื้อหาไฟล์ก่อนที่จะรันสคริปต์:

FirstName

นามสกุล

Vignesh

กฤษณกุมาร

Nandhini

วิเนช

Sampath

ชาร์ลู

Vyapini

วิเนช

Krishnakumar

สังขาร

Durga

กฤษณกุมาร

รหัส:

#Reading the csv by importing
$CSV = import-csv "C:\Users\R003646\Desktop\Sample.csv" | select FirstName,LastName,
@{n='FullName';e={$_.FirstName + " " + $_.LastName}}
# script @{n='FullName";e={$_.FirstName + " " + $_.LastName}} will create the new column FullName and concatenate the FirstName and LastName Column Value
# Export the data from the Variable $CSV to the new file
$CSV | export-CSV "C:\Users\R003646\Desktop\Sample.csv"

เอาต์พุต:

ตัวอย่าง #9

ที่นี่เราจะดูว่าการรวมสองเส้นทางขึ้นไปทำให้เกิดเส้นทางเดียวได้อย่างไร Join-Path cmdlet รวมเส้นทางตั้งแต่สองเส้นทางขึ้นไปไว้ในเส้นทางเดียว

ไวยากรณ์:

NAME
Join-Path
SYNTAX
Join-Path [-Path] <string[]> [-ChildPath] <string> [-Resolve] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>]
ALIASES
None

Eตัวอย่าง #10

อินพุต:

Write-Host "Welcome to Join Path example"
$path1= "C:\test"
$path2="D:\test"
$path3= "E:\test"
$path4="F:\test"
$path5="G:\test"
Write-Host "Appending new path"
Join-Path -Path $path1, $path2, $path3, $path4, $path5 -ChildPath test1

เอาต์พุต:

ฟังก์ชันสตริงอื่น ๆ ที่ใช้กันทั่วไป

1. แยก()

นี่เป็นอีกวิธีหนึ่งที่สามารถใช้เพื่อแยกสตริงออกเป็นสตริงย่อยได้

ไวยากรณ์:

.Split(strSeparator [, MaxSubstrings] [, Options])
String -Split strSeparator [, MaxSubstrings] [, Options]
String -Split {scriptblock} [, MaxSubstrings]
-Split String
  • strSeparator: เป็นอักขระประจำตัวเพื่อแยกสตริง
  • MaxSubstrings: จำนวนสูงสุดของสตริงย่อยที่สามารถสร้างได้

ตัวอย่าง:

Write-Host "Generating substring using split method"
$teststring="My name is Joseph- am from UK"
Write-Host "splitting using - character"
$teststring -split "-"
$teststring="domainname\username"
Write-Host "Splitting using \ character"
$teststring -split "\\"
Write-Host "generating substring using space"
$string="string1 string2 strin3"
$string.Split("")
Write-Host "splitting using multiple separators"
$string="domain\systems-test"
$string.Split("\\").Split("-")

เอาต์พุต:

2. แทนที่ฟังก์ชัน

เมื่อพูดถึงสตริง การแทนที่ส่วนหนึ่งของสตริงหรือสตริงย่อยถือเป็นการดำเนินการที่สำคัญ ผู้ใช้ PowerShell จำเป็นต้องค้นหาข้อความและแทนที่ด้วยข้อความอื่นๆ เสมอ ซึ่งสามารถทำได้โดยวิธีการแทนที่()

ไวยากรณ์:

Replace(strOldChar, strNewChar)
Stroldchar: Character to be found
Strnewchar: character to be replace the found text

ตัวอย่าง:

รหัส:

Write-Host "Replacing a text in string"
$test="Old text to be replaced"
Write-Host "Going to replace old"
$test.Replace("old","New")
Write-Host "Text is replaced"

เอาต์พุต:

สรุป - PowerShell Concatenate String

บทความนี้ครอบคลุมถึงการต่อสตริงโดยละเอียด โดยสาธิตวิธีการต่างๆ ที่สามารถรวมสตริงเข้าด้วยกันได้ นอกเหนือจากตัวดำเนินการ “+” ยังสาธิตตัวอย่างต่างๆ เกี่ยวกับวิธีการเชื่อมต่อสตริงเข้าด้วยกัน นอกจากนี้ยังครอบคลุมวิธีการสตริงต่างๆ เช่น การต่อสตริง การเปรียบเทียบสตริง การแทนที่สตริง ฯลฯ

คำถามที่พบบ่อย (FAQ)

ไตรมาสที่ 1 วิธีที่เป็นไปได้ในการต่อสตริงใน PowerShell คืออะไร

คำตอบ: เข้าถึง 'PowerShell ISE ตั้งแต่แรกด้วยสิทธิ์ของผู้ดูแลระบบ หากต้องการทำเช่นเดียวกัน ให้ไปที่แถบค้นหาเพื่อค้นหา “PowerShell ISE“ หลังจากได้รับผลการค้นหาแล้ว ให้คลิกขวาที่แอปพลิเคชัน 'PowerShell ISE' แล้วเลือก 'เรียกใช้ในฐานะผู้ดูแลระบบ'

ไตรมาสที่ 2 จะแทนที่สตริงใน PowerShell ได้อย่างไร

คำตอบ: ใน PowerShell เป็นเรื่องง่ายที่จะแทนที่สตริงที่มีอยู่ด้วยสตริงใหม่โดยใช้ตัวดำเนินการแทนที่() แบบง่าย คอมไพเลอร์ไม่พบสตริงที่ต้องการ มันไม่ตอบสนองต่อสิ่งใด

ไตรมาสที่ 3 จะแสดงตัวแปรใน PowerShell ได้อย่างไร

คำตอบ: ตัวแปรคือหน่วยจัดเก็บข้อมูล ใน PowerShell ตัวแปรเพียงแสดงสตริงข้อความที่ขึ้นต้นด้วยเครื่องหมายดอลลาร์ ตัวอย่างเช่น $text, $value ฯลฯ

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