Different ways of joining empty strings produce different results

13 ビュー (過去 30 日間)
Samuel Chan
Samuel Chan 2024 年 7 月 18 日
編集済み: DGM 2024 年 7 月 19 日
Here are different ways to join empty strings. One may expect the results to be the same, but in reality, they give 3 different kind of answers. What is the rationale behind it?
join00 = join(string.empty(0,0), "/"); % 0x1 string
join01 = join(string.empty(0,1), "/"); % <missing>
join10 = join(string.empty(1,0), "/"); % <missing>
strjoin00 = strjoin(string.empty(0,0), "/"); % ""
strjoin10 = strjoin(string.empty(1,0), "/"); % ""
strjoin01 = strjoin(string.empty(0,1), "/"); % ""
Background
In my application, multiple series of strings are joined. Sometimes though, certain series is empty, in which case I am tasked to detect those results and replace them with "N/A". Through trial and error, I found that I cannot simply detect those results with only isempty(result), ismissing(result), or (isstring(result) || ischar(result)) && strlength(result) == 0, but I need the combination of all of them, which makes me wonder if there is any reason behind the different behavior.

回答 (1 件)

Anagha Mittal
Anagha Mittal 2024 年 7 月 18 日
Hi Samuel,
The different behaviors of "join" and "strjoin" when handling empty strings in MATLAB stem from how each function processes and interprets empty arrays:
"join" Function:
  • join(string.empty(0,0), "/")": Produces a 0x1 string array. This is because "join" operates along the last dimension that does not equal 1, and here the 0-by-0 array gets transformed into a 0-by-1 array with no elements to join, hence it remains a 0x1 empty string array.
  • join(string.empty(0,1), "/"): Returns <missing>. Since there are no elements to join, MATLAB considers this as a missing value.
  • join(string.empty(1,0), "/"): Also returns <missing>, for the same reason as the 0x1 case.
strjoin Function:
  • strjoin(string.empty(0,0), "/"): Returns an empty string "". "strjoin" concatenates elements with the delimiter, but with no elements to process, it simply returns an empty string.
  • strjoin(string.empty(1,0), "/"): Also returns an empty string "" because the 1-by-0 array has no elements to concatenate.
  • strjoin(string.empty(0,1), "/"): Similarly returns an empty string "".
The key difference is how "join" and "strjoin" handle empty arrays:
  • join: Designed to join elements of a string array along a specified dimension, and when no elements exist, it may produce an empty string array or <missing> values depending on the input dimensions.
  • strjoin: Primarily used to concatenate strings with a specified delimiter, and when provided with empty arrays, it defaults to returning an empty string.
In your application, detecting empty results correctly requires combining checks like "isempty", "ismissing", and "strlength":
For handling empty string results robustly in your code, you might use:
if isempty(result) || ismissing(result) || (isstring(result) && strlength(result) == 0)
result = "N/A";
end
For more information on "join" and "strjoin", refer to the following:
Hope this is helpful!
  2 件のコメント
Samuel Chan
Samuel Chan 2024 年 7 月 19 日
編集済み: Samuel Chan 2024 年 7 月 19 日
Hi Anagha,
Thanks for the answer.
Just curious - is your answer mostly AI generated?
Anyway, following the logic in your answer:
  • "Since there are no elements to join, MATLAB considers this as a missing value": if that is true, then given all 6 lines of code has no element to join, they should all return <missing>. But that's not the case.
  • "returns an empty string "" because the 1-by-0 array has no elements to concatenate": if that is true, then given all 6 lines of code has no element to concatnate, then they should all return "". But that's not the case.
So there remains questions to be explained.
DGM
DGM 2024 年 7 月 19 日
編集済み: DGM 2024 年 7 月 19 日
At least for me, the question of "what is the reason" isn't answered by "because the code does so and so". It's answered by an insight into the purpose and intent behind that piece of code. Maybe that can be inferred from the code, but I'm not seeing it.

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by