left characters of a string

조회 수: 118 (최근 30일)
Danielle Leblanc
Danielle Leblanc 2011년 7월 6일
답변: Steven Lord 2022년 11월 26일
if I have a name 'Microsoft', how can i get the first 6 characters 'Micros'?

채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 7월 6일
name = 'Microsoft'
out = name(1:6)
  댓글 수: 2
Real User
Real User 2022년 11월 26일
편집: Real User 2022년 11월 26일
What if name has < 6 characters?
Is there some short way or do have have to write
out = name(1:min(6,length(name)));
substr seems to require some stateflow package https://se.mathworks.com/help/stateflow/ref/substr.html
Stephen23
Stephen23 2022년 11월 26일
name = 'Microsoft';
name(1:min(end,6))
ans = 'Micros'
name = 'Cat';
name(1:min(end,6))
ans = 'Cat'

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

추가 답변 (1개)

Steven Lord
Steven Lord 2022년 11월 26일
This wasn't an option when the question was originally asked, but the extractBefore function will work for both char vectors and string arrays.
c = 'Microsoft'
c = 'Microsoft'
c6 = extractBefore(c, 7)
c6 = 'Micros'
s = string(c)
s = "Microsoft"
s6 = extractBefore(s, 7)
s6 = "Micros"

카테고리

Help CenterFile Exchange에서 Stateflow에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by