もっと詳しく

PowerShell String は、文字列データを格納し、文字列操作を処理するさまざまなメソッドを提供します。 文字列またはファイル内の文字列または文字の置換は、そのような文字列操作の 1 つです。 PowerShell には 交換() 文字列の置換を実現するメソッド。

この記事では、PowerShell 文字列に対して PowerShell replace() メソッドを使用し、文字列内の文字を置き換える方法について説明します。

PowerShell の Replace() メソッド

交換() メソッドは、文字列内の部分文字列を置き換えます。 古い値を見つけて、新しい値に置き換えます。

構文

string Replace(char oldValue, char newValue)

string Replace(string oldValue, string newValue)

パラメーター:

古い値: 文字列内で検索する文字または文字列。

新しい値: 見つかったテキストを置き換える文字または文字列。

出力:

文字列置換後の文字列を返します。

Powershell Replace() を使用して文字を置き換えます

PowerShell の使用 交換() メソッドを使用すると、文字を新しい値に簡単に置き換えることができます。

パワーシェル 交換() メソッドには、検索する文字列または文字と、検索テキストを置き換える文字列の 2 つの引数があります。

$str = "Welcome!Admin to ShellGeek."  

 $str.Replace('!',',')   

上記の PowerShell スクリプトでは、 $str 変数は文字列データを格納します。 文字を置き換えるには ! 指定された文字列で コンマ $str 変数に対して replace() メソッドを呼び出します。

交換() メソッドは 2 つの引数 char を取ります ! 指定された文字列と文字を検索する PowerShell 文字列の検索文字に置き換えます。

文字を新しい文字に置き換えた後の上記の PowerShell スクリプトの出力は次のとおりです。

replace() を使用して PowerShell の文字列を置き換えます

PowerShell 文字列を使用できます 交換() 文字列内のテキストを置き換える関数。 検索する文字列と、見つかったテキストに置き換える文字列として 2 つの引数を受け取ります。

$str = "This is example about PowerShell string split method" 
 
$str.Replace("split","replace")   

上記の PowerShell スクリプトでは、 $str 変数は文字列データを格納します。 テキストを置き換えるにはスプリット」を含む特定の文字列交換「、PowerShellを呼び出します 交換() 方法。

テキストを新しい文字列値に置き換え、以下に示す文字列を返します

PS C:> $str = "This is example about PowerShell string split method"                                                   

PS C:> $str.Replace("split","replace")                                                                                 

This is example about PowerShell string replace method

PS C:>   

結論

PowerShell の string replace() メソッドを使用して文字列または文字列内の文字列を置換する方法に関する上記の記事がお役に立てば幸いです。

PowerShell Active Directory コマンドと PowerShell の基本に関するその他のトピックについては、ShellGeek のホームページを参照してください。

おすすめコンテンツ

PowerShell は、文字列内のテキストを置き換えます

PowerShell 置換部分文字列

The post PowerShell Replace – 文字列を置換するために使用する方法 appeared first on Gamingsym Japan.