How to extract a certain part from a string?

2 ビュー (過去 30 日間)
Wei Feng Hsu
Wei Feng Hsu 2017 年 5 月 17 日
コメント済み: Wei Feng Hsu 2017 年 5 月 18 日
Hi, I have a string variable with 9-digit value (not necessarily integer,could be capitalized-letter),
and I want to transform them into 8-digit value by removing the last digit value.
For example, my input will be some string as
'0036R3879' '0034658A6'
and respectively they should be transformed into
'0036R387' '0034658A'
such that the last digit is removed and the output is still a sting with 8 digit.
I don't know how to operate with string in this case, and the function 'strrep' seems not to work here.
Can someone familiar with string operation help me out?
Thank you!

採用された回答

Guillaume
Guillaume 2017 年 5 月 17 日
Assuming R2016b or later:
extractBefore(yourstringarray, 9)
  3 件のコメント
Guillaume
Guillaume 2017 年 5 月 18 日
Hum, no. My documentation says that it was introduced in R2016b. The behaviour was changed in R2017a to return the same data type as the first input whereas in R2016b, it always returned string arrays.
Wei Feng Hsu
Wei Feng Hsu 2017 年 5 月 18 日
It works! Thank you

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2017 年 5 月 17 日
Try this:
myStrings = string({'0036R3879'; '0034658A6'; '00300658A6'});
% Print to command window:
t = table(myStrings)
for row = 1 : size(t, 1)
% For every row...
oldString = t.myStrings{row};
t.myStrings{row} = oldString(1:end-1);
end
% Print to command window:
t
Hopefully it's rather self explanatory.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by