2017-02-22 9 views
0

値が22_ABCDの の文字列があります。これで私のパラメータにはABCDだけが必要です。 PowerShellでこれを行う最善の方法は何ですか?Powershell文字列セパレーション

Split()を使用して$ stringvalue = Split [1]を使用する必要がありますか?または、これを行うpowershellに関数がありますか?

+0

すべての文字列が同じ形式(## _ AAAA)を持っているのだろうか?もう少し詳しくお聞かせください。 –

答えて

2

Splitは、このようにして使用できます。ここで、角括弧内の値は分割に使用する文字です。上記のコードを実行

$string = "22_ABCD" 
$string.Split("_") 

は、2つのアイテムを含む配列を出力:

22 
ABCD 

その後[1]と配列の2番目の項目を参照することができ([0]は、最初のアイテムである)このような:

$string.Split("_")[1] 

次の項目だけを出力します。

ABCD 
+0

偉大な、あなたは文字列を直接分割することができたか分からなかった。それは素晴らしいことです :) – Kevin

0

正規表現は、アンダースコアの後の次の文字セットではなく、英数字を検索する可能性があります。

$x = '22_ABCD_FTG_3' 
[regex]::match($x,'([A-Z)]+)').Groups[1].Value 
0
#method 0, with split operator 
$Elements="22_ABCD" -split "_" 
$Elements[0] 
$Elements[1] 

#method 1, with split member 
$Elements="22_ABCD".Split('_') 
$Elements[0] 
$Elements[1] 

#method 2, with split member and direct affectation 
$Element1, $Element2="22_ABCD".Split('_') 
$Element1 
$Element2 

#method 3, with ConvertFrom-String 
$Elements="22_ABCD" | ConvertFrom-String -Delimiter "_" -PropertyNames "Element1", "Element2" 
$Elements.Element1 
$Elements.Element2 

#method 4, with ConvertFrom-Csv 
$Elements="22_ABCD" | ConvertFrom-Csv -Delimiter "_" -Header "Element1", "Element2" 
$Elements.Element1 
$Elements.Element2 

#method 5, with regex 
$Elements=[regex]::split("22_ABCD", '_') 
$Elements[0] 
$Elements[1] 
関連する問題