Skip to main content

This is documentation for Caché & Ensemble. See the InterSystems IRIS version of this content.Opens in a new tab

For information on migrating to InterSystems IRISOpens in a new tab, see Why Migrate to InterSystems IRIS?

Replace

指定された文字列を指定された回数だけ別の部分文字列で置換した文字列を返します。

Synopsis

Replace(string,find,replacewith[,start[,count[,compare]]])

引数

string 置換する部分文字列を含む文字列式。
find 検索対象の部分文字列。見つかった場合、replacewith に指定した文字列に置き換えられます。
replacewith 置換文字列。
start オプション — 部分文字列の検索を開始する string 内の位置。省略した場合、1 と見なされます。
count オプション — 部分文字列を置換する回数。省略した場合、既定値は -1 です。これは、可能なすべての置換を実行します。start と併用する必要があります。
compare オプション — 部分文字列を比較する際に使用する、比較の種類を示す数値。値については、概要を参照してください。省略した場合、既定値は 0 です。これは、バイナリ比較を実行します。

概要

引数 compare の値は以下のとおりです。

定数 説明
vbBinaryCompare 0 大文字と小文字を区別する (バイナリ) 比較を実行します
vbTextCompare 1 大文字と小文字を区別しないテキスト比較を実行します。
Note:

大文字と小文字を区別しない比較は、すべての Caché 対応ロケールで予測どおりに機能します。

Replace 関数は、以下の値を返します。

条件 Replace の返り値
string の長さがゼロの場合 長さゼロの文字列 ("")
find の長さがゼロの場合 string に指定した文字列のコピー
replacewith の長さがゼロの場合 string に指定した文字列のコピーから、find に指定した部分文字列をすべて削除したもの
start の値が string の長さより大きい場合 長さゼロの文字列 ("")
count が 0 の場合 string に指定した文字列のコピー

Replace 関数の返り値は、置換が実行された部分文字列であり、start に指定した位置から始まり、式の文字列の末尾で終了します。

以下の例は、find 部分文字列を文字列の先頭から、指定された回数だけ、またはすべて (既定) 置換します。

Println Replace("To ski or not to ski","ski","be")
Println Replace("To ski or not to ski","ski","be",1,2)
Println Replace("To ski or not to ski","ski","be",1,1)

以下の例は、find 部分文字列を、文字列の指定された位置から、指定された回数だけ、またはすべて (既定) 置換します。

Println Replace("To ski or not to ski","ski","be",4,2)
Println Replace("To ski or not to ski","ski","be",4,1)

返り値は、置換実行後の元の文字列ではなく、4 番目のパラメータ (start) に指定した位置から始まる部分文字列です。

以下の例は、バイナリおよびテキスト比較を実行します。

' A binary comparison starting at the beginning
' of the string. Returns "XXYXXPXXY". 
Println Replace("XXpXXPXXp", "p", "Y")

' A textual comparison starting at position 3.
' Returns "YXXYXXY".
Println Replace("XXpXXPXXp", "p", "Y", 3, -1, 1)
FeedbackOpens in a new tab