Repeat a string with a delimiter

112 visualizzazioni (ultimi 30 giorni)
Yuzhen Lu
Yuzhen Lu il 7 Gen 2021
Modificato: Stephen23 il 7 Gen 2021
Assuming a string
A = 'abc';
How to repeat it for several times, say 3 times, with a delimiter ',', so that to obtain:
'abc, abc, abc'
Is there a neat solution to do so?

Risposte (1)

Stephen23
Stephen23 il 7 Gen 2021
Modificato: Stephen23 il 7 Gen 2021
A = 'abc';
B = join(repmat({A},1,3),', ');
B = B{1}
B = 'abc, abc, abc'
C = join(repmat(string(A),1,3),', ') % string output!
C = "abc, abc, abc"
D = sprintf('%1$s, %1$s, %1$s',A)
D = 'abc, abc, abc'

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by