Why is isstr not recommended?

3 ビュー (過去 30 日間)
Josh G.
Josh G. 2019 年 1 月 25 日
コメント済み: Star Strider 2019 年 1 月 28 日
When using isstr in my code, I get a warning that it is not recommended and I should use ischar instead. But ischar doesn't provide the same functionality as isstr. For a string, ischar returns 0 and isstr returns 1, while for a character array, ischar returns 1 and isstr returns 1. There doesn't seem to be any replacement for isstr that provides the same functionality. Why is this warning displayed?

採用された回答

Star Strider
Star Strider 2019 年 1 月 25 日
The string (link) datatype was introduced in R2016b, along with a number of functions to work with it, of which isstring (link) is one. I suspect the recommendations for isstr changed to be compatible with the new datatype, and avoid any ambiguity.
  2 件のコメント
Josh G.
Josh G. 2019 年 1 月 28 日
I wasn't aware of isstring, but that looks like a better replacement. I wonder why Mathworks recommends ischar though instead of isstring.
Star Strider
Star Strider 2019 年 1 月 28 日
I don’t believe MathWorks is actually ‘recommending’ one over the other, just doing its best to avoid ambiguity. It very much depends on what you’re testing.
Example —
s = "string!";
c = 'character!';
ischar_test = [ischar(s) ischar(c)]
isstring_test = [isstring(s) isstring(c)]
isstr_test = [isstr(s) isstr(c)]
produces:
ischar_test =
1×2 logical array
0 1
isstring_test =
1×2 logical array
1 0
isstr_test =
1×2 logical array
0 1
This also illustrates the problem with isstr.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by