Replace each characters of strings with '-'
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, I want to replace all the characters of a string by '-', say, flowers will be like '-------'. Please tell me how am I supposed to do that. I am totally new to matlab. As a beginner I've done this so far.
%Select a word
B='skyyiy'
%input letter
c='y'
%stringlength
l=strlength(B)
%indices of letters occurring
d=strfind(B,c)
newStr=strrep(B,c,'-')
2 commentaires
Réponse acceptée
KL
le 10 Août 2017
if you've no conditions but just to have the same length as the old string then maybe something like
>> B='skyyiy'
newStr = B;
newStr(1:end) = '-'
B =
'skyyiy'
newStr =
'------'
0 commentaires
Plus de réponses (1)
Walter Roberson
le 10 Août 2017
regexprep(B, c, '-')
or
B(B==c) = '_';
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!