Can I pull a single character froma string?

조회 수: 42 (최근 30일)
Alex
Alex 2017년 9월 15일
답변: James Tursa 2017년 9월 15일
I'm trying to pull a single character from a user defined string.
A = "Hello, World";
B = "W";
I have A, I want to get B from it. I have tried
B = A(end-3);
as well as
B = A(8);
the former gave me the following error "Subscript indices must either be real positive integers or logicals," and the latter worked about as well as is expected.
  댓글 수: 1
Stephen23
Stephen23 2017년 9월 15일
편집: Stephen23 2017년 9월 15일
The whole point of strings is that they are atomic, and each string constitutes one whole element of a string array. So what you are doing is a bit contradictory to the concept of a string. Of course getting one character of a character array is trivially easy, so you could just convert the string to a char array and use indexing.

댓글을 달려면 로그인하십시오.

답변 (1개)

James Tursa
James Tursa 2017년 9월 15일

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by