13 Ocak 2021 Çarşamba

Powershell String

split metodu ve split operator Farklıdır
- split() metodu verilen tüm girdiyi tek tek karakterler gibi değerlendirir ve ona göre böler. Ayrıca büyük küçük fark farklına duyarlıdır
- split operator ise verilen tüm girdiyi tek bir ayraç olarak algılar. Ayrıca büyük küçük fark farklına duyarlı değildir.
Örnek
Elimizde şöyle bir kod olsun
$Str="This is the<BR />source string<BR />ALL RIGHT"
$Str.Split("<BR />")
This
is
the
(multiple blank lines)
source
string
(multiple blank lines)
ALL
IGHT
$Str -Split("<BR />")
This is the
source string
ALL RIGHT 
Açıklaması şöyle
From this you can see that the string.split() method:

- performs a case sensitive split (note that "ALL RIGHT" his split on the "R" but "broken" is not split on the "r")
- treats the string as a list of possible characters to split on

While the -split operator:

- performs a case-insensitive comparison
- only splits on the whole string
split metodu
Örnek
Şöyle yaparız. Split sonucu direkt değişkenlere atanıyor.
$string = 'FirstPart SecondPart'
$a,$b = $string.split(' ')
$a
$b
Örnek
Şöyle yaparız. Burada | ile ayrılmış alanlar split edildikten sonra alanlar tekrar sıralanıp yine | ile birleştiriliyor. Yani aslında sütunların sıralaması değiştiriliyor.
$File = get-content "D:\test\1234.txt"
$OutputFile = foreach($line in $File){($line.split('|'))[0,4,1,2,3,5] -Join '|'}
$OutputFile | out-file -filepath "D:\test\4321.txt" -Encoding "ascii"
split operator
Örnek
Şöyle yaparız. Çıktı olarak "1stZAP/SFR" alırız
$MilitaryDude = "Doe, Jane C LTC MMUA DCC 1stZAP/SFR"
($MilitaryDude -split "DCC" | Select -First 1).Trim()

Hiç yorum yok:

Yorum Gönder