Concatenating strings w/ character with while loops

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by