How to compact a string?

1 visualizzazione (ultimi 30 giorni)
pamela sulis
pamela sulis il 11 Nov 2015
Commentato: pamela sulis il 11 Nov 2015
Hi! I have a sting 'aaaaaaabccccbbbdcccccaaaa' and I want to trasform it in 'abcbdca': I want to have only one of the consecutive equal value. Can you give me some suggests? Thanks

Risposta accettata

Walter Roberson
Walter Roberson il 11 Nov 2015
Modificato: Walter Roberson il 11 Nov 2015
s = 'aaaaaaabccccbbbdcccccaaaa'
new_s = regexprep(s, '(.)(\1)+', '$1');
  3 Commenti
Stephen23
Stephen23 il 11 Nov 2015
Modificato: Stephen23 il 11 Nov 2015
@pamela sulis: read the regexp documentation to learn the answer to your question: it describes the input str as "Input text, specified as a string or a cell array of strings." Struct is not listed.
pamela sulis
pamela sulis il 11 Nov 2015
I use it in a struct and it give me a correct answer!

Accedi per commentare.

Più risposte (0)

Categorie

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

Community Treasure Hunt

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

Start Hunting!

Translated by