How do I add variables into a string within a table?

2 ビュー (過去 30 日間)
Nick O'Connor
Nick O'Connor 2020 年 10 月 20 日
コメント済み: Star Strider 2020 年 10 月 20 日
I have a table with a column of 10 digit strings that I need to add "-" into.
How do I turn a column of strings looking like this "0001336363" into a column of strings looking like this "0001336-36-3"?

採用された回答

Star Strider
Star Strider 2020 年 10 月 20 日
The only option appears to be to turn the string variable into a char array first:
str = "0001336363";
strc = char(str);
Out = sprintf("%7s-%2s-%s", strc(1:7),strc(8:9),strc(10))
producing:
Out =
"0001336-36-3"
.
  2 件のコメント
Nick O'Connor
Nick O'Connor 2020 年 10 月 20 日
Thank you. I was trying to come up with something along that line but hadn't thought to change it to a character array.
After asking here, I found this alternative solution.
tests.CAS12 = insertAfter(tests.CAS12,7,"-"); % Add in the first "-"
tests.CAS12 = insertAfter(tests.CAS12,10,"-") % Add in the second "-".
I shall compare the two and decide which I think is cleaner.
Star Strider
Star Strider 2020 年 10 月 20 日
As always, my pleasure!
I searched through the string functions, although I didn’t consider that insertAfter would apply. It would likely come down to which is the more efficient use of computer resources.

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

その他の回答 (0 件)

カテゴリ

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