Programming with MATLAB | ![]() ![]() |
検索と置換
MATLABは文字列の中でキャラクタの検索や置換をするいくつかの関数を用意しています。label
と名付けた文字列を考えましょう。
label = 'Sample 1, 10/28/95';
関数 strrep
は、標準の検索、置換演算を行ないます。例えば、日付を'10/28'
から'10/30'
に変更するのにstrrep
を使います。
newlabel = strrep(label,'28','30') newlabel =
Sample 1, 10/30/95
findstr
は、長い文字列の中の文字列に準ずるものの始まる位置を出力します。label
の中に'amp'
文字列の中のものが生じる位置を求めるには、
position = findstr('amp',label) position =
2
'amp'
がlabel
の中で表れる位置は、2番目のキャラクタです。
関数strtok
は、入力文字列の中で、最初に分離キャラクタが表れる位置の前のキャラクタを出力します。デフォルトの分離キャラクタは、ホワイトスペースキャラクタの集合です。ユーザは、strtok
を使って、文章を言葉に分割できます。例えば、
function all_words = words(input_string) remainder = input_string; all_words = ''; while (any(remainder)) [chopped,remainder] = strtok(remainder); all_words = strvcat(all_words,chopped); end
関数 strmatch
は、キャラクタ配列または文字列のセル配列の行を調べ、与えられた一連のキャラクタで始まる文字列を見つけます。それらのキャラクタで始まる行のインデックスを出力します。
maxstrings = strvcat('max','minimax','maximum') maxstrings = max minimax maximum strmatch('max',maxstrings) ans = 1 3
![]() |
文字列内のキャラクタ分類 | 文字列/数字の変換 | ![]() |