I want to grab the first char of a var string and the first char of the following caracter
Example:
$var1 = "Jean-Martin"
I want a way to grab the first letter "J" then I want to take the first char following the "-" (dash) which is "M".
I want to grab the first char of a var string and the first char of the following caracter
Example:
$var1 = "Jean-Martin"
I want a way to grab the first letter "J" then I want to take the first char following the "-" (dash) which is "M".
このようなもの?
$initial1 = $var1[0]
$initial2 = $var1.Split('-')[1][0]
Powershell の文字列は、.Net フレームワークの System.String クラスを使用します。そのため、個々の文字を取得するためにインデックスを作成でき、上記で使用した Split メソッドなど、多くのメソッドを利用できます。
こちらのドキュメントを参照してください。
$var1 = "Jean-Martin"
最初の文字を取得するには:
$var1[0]
ダッシュの後の最初の文字を取得するには:
$characterToSeek = '-'
$var1[$var1.IndexOf($characterToSeek)+1]
正規表現を使用する別のオプション:
PS> $var1 -replace '^(.)[^-]+-(.).+$','$1$2'
JM