Why does implicit conversion happen from 'char' to 'string' when concatenating?
조회 수: 3 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2022년 12월 1일
답변: MathWorks Support Team
2022년 12월 1일
Concatenating char arrays results in a new char array, as expected:
>> ['a', 'b', 'c']
ans =
'abc'
However, if any element of this array is a string, there is an implicit conversion of all the char arrays to strings, and the behavior is quite different:
>> ['a', "b", 'c']
ans =
1×3 string array
"a" "b" "c"
What is the reason for this?
채택된 답변
MathWorks Support Team
2022년 12월 1일
In concatenation operations, the dominant object determines the class of the resulting array, according to the following documentation:
https://www.mathworks.com/help/matlab/matlab_oop/implicit-class-conversion.html#bvo4poc
Therefore, when the array contains both 'char' and 'string', the concatenation results in a 'string' array.
The following documentation includes other related examples to demonstrate this idea:
https://www.mathworks.com/help/matlab/matlab_prog/concatenation-examples.html
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!