フィルターのクリア

Concatenating strings w/ character with while loops

4 ビュー (過去 30 日間)
Nathen Eberhardt
Nathen Eberhardt 2020 年 2 月 9 日
編集済み: dpb 2020 年 2 月 9 日
I need to concatenate the elements of firstname until it contains the whole first name(nate) of myname.
myname = 'Nate Johnson';
firstname = '';
length(myname)
f = strcat(myname, g);
while length(myname) < 7
firstname = myname;
x = myname;
end
disp(x)
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 2 月 9 日
while length(myname) < 7
firstname = myname;
x = myname;
end
Inside your loop, you are not changing myname, so if your loop starts going at all, your loop will never end.

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

採用された回答

dpb
dpb 2020 年 2 月 9 日
編集済み: dpb 2020 年 2 月 9 日
>> myname = 'Nate Johnson';
>> split(myname)
ans =
2×1 cell array
{'Nate' }
{'Johnson'}
>>
presuming the next step would be to also isolate lastname.
Or, alternatively
>> firstname=extractBefore(myname,' ')
firstname =
'Nate'
>>

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by