left characters of a string

97 ビュー (過去 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 Center および File ExchangeStateflow についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by