how can I remove 4 character of a string?

10 ビュー (過去 30 日間)
sara adam
sara adam 2018 年 11 月 20 日
コメント済み: Jan 2021 年 8 月 30 日
for example string is LASTNAME ,output=NAME

回答 (2 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 11 月 20 日
編集済み: KALYAN ACHARJYA 2018 年 11 月 20 日
s='lastname';
modified_s=s(5:end);
%Command Window
>> s='lastname'
>> modified_s=s(5:end)
s=lastname
modified_s=name
  1 件のコメント
Jan
Jan 2018 年 11 月 20 日
s(5:end) can be interpreted as: Keep the last part. This is the same as removing the leading 4 characters:
s = 'lastname';
s(1:4) = [];

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


John Cunningham
John Cunningham 2021 年 8 月 26 日
If s needs to be a string, just convert to character, grab the indices you need, then convert back to string.
K>> s = "lastname";
K>> s = char(s);
K>> s = string(s(5:end))
s =
"name"
  2 件のコメント
Stephen23
Stephen23 2021 年 8 月 27 日
編集済み: Stephen23 2021 年 8 月 27 日
Simpler and more efficient to use indexing to access the character vector that is already inside the string container:
s = "lastname";
s{1} = s{1}(5:end)
s = "name"
No CHAR call, no STRING call required.
Jan
Jan 2021 年 8 月 30 日
Or:
s = "lastname";
t = extractAfter(s, 4)

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

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by