Main Content

replaceBetween

始点と終点の間の部分文字列の置換

説明

newStr = replaceBetween(str,startPat,endPat,newText) は、str 内の部分文字列を newText 内のテキストで置き換えます。置き換えられる部分文字列は、部分文字列 startPatendPat の間に出現します。ただし、replaceBetweenstartPat および endPat 自体は置き換えません。replaceBetween は結果を newStr として返します。引数 newText は置き換える部分文字列とは異なる文字数をもつことができます。

str が string 配列または文字ベクトルの cell 配列の場合、replaceBetweenstr の各要素の部分文字列を置き換えます。出力引数 newStr のデータ型は、str と同じです。

newStr = replaceBetween(str,startPos,endPos,newText) は、str 内の部分文字列を置き換えます。置き換えられる部分文字列は、str 内の startPosendPos の間の位置に出現し、これらの位置にある文字を含みます。

newStr = replaceBetween(___,'Boundaries',bounds) は、前述の任意の構文で指定した始点と終点について、含めるか除外するかを指示します。bounds'inclusive' の場合はそれらを含め、bounds'exclusive' の場合は除外します。たとえば、replaceBetween(str,startPat,endPat,newText,'Boundaries','inclusive') は、startPatendPat およびそれらの間にあるすべてのテキストを newText で指定されたテキストに置き換えます。

すべて折りたたむ

string 配列を作成し、部分文字列の間に出現するテキストを置き換えます。

string は二重引用符を使用して作成できます。

str = "The quick brown fox"
str = 
"The quick brown fox"

部分文字列 quickfox の間に出現するテキストを置き換えます。関数 replaceBetween は、出力でテキストを置き換えますが、quickfox は置き換えません。

newStr = replaceBetween(str,"quick "," fox","red")
newStr = 
"The quick red fox"

string 配列の各要素にある部分文字列を置き換えます。開始インジケーターと終了インジケーターとして異なる部分文字列を指定する場合、それらを含む string 配列または cell 配列は、str と同じサイズでなければなりません。置換テキストも、同じサイズの string 配列または cell 配列内になければなりません。

str = ["The quick brown fox jumps";"over the lazy dog"]
str = 2x1 string
    "The quick brown fox jumps"
    "over the lazy dog"

newText = ["red";"sleeping"];
newStr = replaceBetween(str,["quick ";"the "],[" fox";" dog"],newText)
newStr = 2x1 string
    "The quick red fox jumps"
    "over the sleeping dog"

R2020b 以降

"internationalization" について "i18n" のような省略形を作成します。これを行うには、単語の最初と最後の文字の間のテキストを文字数に置き換えます。

まず、string 配列を作成します。

str = ["globalization";
       "internationalization";
       "localization"]
str = 3x1 string
    "globalization"
    "internationalization"
    "localization"

文字列の最初と最後の文字が一致するパターンを作成します。関数 textBoundary は文字列の最初と最後の境界に一致しますが、呼び出し lettersPattern(1) は任意の文字と一致します。

startPat = textBoundary + lettersPattern(1)
startPat = pattern
  Matching:

    textBoundary + lettersPattern(1)

endPat = lettersPattern(1) + textBoundary
endPat = pattern
  Matching:

    lettersPattern(1) + textBoundary

str 内の各 string の長さを求めます。最初と最後の文字を考慮して、各長さから 2 を減算します。長さを文字列に変換します。

L = strlength(str) - 2;
L = string(L)
L = 3x1 string
    "11"
    "18"
    "10"

str の要素の省略形を作成します。最初と最後の文字の間の文字を L の要素に置き換えます。

newStr = replaceBetween(str,startPat,endPat,L)
newStr = 3x1 string
    "g11n"
    "i18n"
    "l10n"

pattern オブジェクトを作成する関数の一覧については、patternを参照してください。

string 配列を作成し、数値で指定した開始位置と終了位置の間の部分文字列を置き換えます。

string は二重引用符を使用して作成できます。名前を含む string を作成します。ミドル ネームを置き換えるため、string の 7 番目と 11 番目の位置を指定します。

str = "Edgar Allen Poe"
str = 
"Edgar Allen Poe"
newStr = replaceBetween(str,7,11,'A.')
newStr = 
"Edgar A. Poe"

string 配列の各要素にある部分文字列を置き換えます。数値配列を使用して異なる開始位置と終了位置を指定する場合、数値配列は入力 string 配列と同じサイズでなければなりません。置換テキストも、同じサイズの string 配列または cell 配列内になければなりません。

str = ["Edgar Allen Poe";"Louisa May Alcott"]
str = 2x1 string
    "Edgar Allen Poe"
    "Louisa May Alcott"

newText = ["A.";"M."];
newStr = replaceBetween(str,[7;8],[11;10],newText)
newStr = 2x1 string
    "Edgar A. Poe"
    "Louisa M. Alcott"

境界値を含めるかどうかを強制的に指定して string 配列の境界内のテキストを置き換えます。包含境界の場合、replaceBetween はテキストと共に境界も置き換えます。排他境界の場合、replaceBetween は境界を置き換えません。

string は二重引用符を使用して作成できます。

str = "small|medium|large"
str = 
"small|medium|large"

6 番目と 13 番目の位置の間にあるテキストを置き換えます。これらの位置にある文字は置き換えません。

newText = "regular";
newStr = replaceBetween(str,6,13,newText,'Boundaries','exclusive')
newStr = 
"small|regular|large"

2 つの部分文字列の間にあるテキストと、これらの部分文字列自体も置き換えます。

str = "The quick brown fox jumps over the lazy dog"
str = 
"The quick brown fox jumps over the lazy dog"
newText = "red bird flies";
newStr = replaceBetween(str,'brown','jumps',newText,'Boundaries','inclusive')
newStr = 
"The quick red bird flies over the lazy dog"

文字ベクトルを作成し、開始位置と終了位置の間のテキストを置き換えます。

chr = 'mushrooms, peppers, and onions'
chr = 
'mushrooms, peppers, and onions'
newChr = replaceBetween(chr,12,18,'pineapple')
newChr = 
'mushrooms, pineapple, and onions'

部分文字列の間のテキストを置き換えます。

newChr = replaceBetween(chr,'mushrooms,',' and',' extra cheese,')
newChr = 
'mushrooms, extra cheese, and onions'

入力引数

すべて折りたたむ

入力テキスト。string 配列、文字ベクトルまたは文字ベクトルの cell 配列として指定します。

置き換えるテキストの開始位置をマークするテキストまたはパターン。次のいずれかとして指定します。

  • string 配列

  • 文字ベクトル

  • 文字ベクトルの cell 配列

  • pattern 配列 "(R2020b 以降)"

str が string 配列または文字ベクトルの cell 配列の場合、str の各要素の部分文字列を置き換えることができます。すべての部分文字列の開始位置が同じであるか、あるいは str の要素ごとに開始位置が異なるかを指定できます。

  • 同じ開始位置を指定するには、startPat を文字ベクトル、string スカラー、または pattern オブジェクトとして指定します。

  • 異なる開始位置を指定するには、startPat を string 配列、文字ベクトルの cell 配列、または pattern 配列として指定します。

例: replaceBetween(str,"AB","YZ",newText) は、str の各要素にある ABYZ の間の部分文字列を newText で指定したテキストに置き換えます。

例: str21 列の string 配列の場合、replaceBetween(str,["AB";"FG"],["YZ";"ST"],newText) は、str(1) 内にある ABYZ の間の部分文字列、および str(2) 内にある FGST の間の部分文字列を置き換えます。

置き換えるテキストの終了位置をマークするテキストまたはパターン。次のいずれかとして指定します。

  • string 配列

  • 文字ベクトル

  • 文字ベクトルの cell 配列

  • pattern 配列 "(R2020b 以降)"

str が string 配列または文字ベクトルの cell 配列の場合、str の各要素の部分文字列を置き換えることができます。すべての部分文字列の終了位置が同じであるか、あるいは str の要素ごとに終了位置が異なるかを指定できます。

  • 同じ終了位置を指定するには、endPat を文字ベクトル、string スカラー、または pattern オブジェクトとして指定します。

  • 異なる終了位置を指定するには、endPat を string 配列、文字ベクトルの cell 配列、または pattern 配列として指定します。

例: replaceBetween(str,"AB","YZ",newText) は、str の各要素にある ABYZ の間の部分文字列を newText で指定したテキストに置き換えます。

例: str21 列の string 配列の場合、replaceBetween(str,["AB";"FG"],["YZ";"ST"],newText) は、str(1) 内にある ABYZ の間の部分文字列、および str(2) 内にある FGST の間の部分文字列を置き換えます。

開始位置。数値配列として指定します。

str が string 配列または文字ベクトルの cell 配列の場合、startPos は数値スカラー、または str と同じサイズの数値配列にすることができます。

例: replaceBetween(str,5,9,newText) は、str の各要素の 5 ~ 9 番目の位置にある部分文字列を newText で指定されたテキストに置き換えます。

例: str21 列の string 配列の場合、replaceBetween(str,[5;10],[9;21],newText) は、str(1) の 5 ~ 9 番目の位置にある部分文字列と、str(2) の 10 ~ 21 番目の位置にある部分文字列を置き換えます。

終了位置。数値配列として指定します。

str が string 配列または文字ベクトルの cell 配列の場合、endPos は数値スカラー、または str と同じサイズの数値配列にすることができます。

例: replaceBetween(str,5,9,newText) は、str の各要素の 5 ~ 9 番目の位置にある部分文字列を newText で指定されたテキストに置き換えます。

例: str21 列の string 配列の場合、replaceBetween(str,[5;10],[9;21],newText) は、str(1) の 5 ~ 9 番目の位置にある部分文字列と、str(2) の 10 ~ 21 番目の位置にある部分文字列を置き換えます。

置換テキスト。string 配列、文字ベクトルまたは文字ベクトルの cell 配列として指定します。

str が string 配列または文字ベクトルの cell 配列の場合、newText は、文字ベクトル、string スカラー、str と同じサイズの string 配列または cell 配列のいずれかとして指定できます。

例: replaceBetween(str,"AB","YZ","efg")str の各要素で ABYZ の間にある部分文字列を efg に置き換えます。

例: str21 列の string 配列である場合、replaceBetween(str,["AB";"FG"],["YZ";"ST"],["efg";"lmnop"]) は、str(1) 内にある ABYZ の間の部分文字列を efg に置き換え、str(2) 内にある FGST の間の部分文字列を lmnop に置き換えます。

境界の動作。'inclusive' または 'exclusive' として指定します。境界の動作が inclusive である場合、前の引数で指定された始点と終点は置き換え対象のテキストに含められます。境界の動作が exclusive である場合、開始位置と終了位置は含まれません。

出力引数

すべて折りたたむ

出力テキスト。string 配列、文字ベクトルまたは文字ベクトルの cell 配列として返されます。strnewStr は同じデータ型です。

拡張機能

バージョン履歴

R2016b で導入