Filter löschen
Filter löschen

Replace each characters of strings with '-'

2 Ansichten (letzte 30 Tage)
Suresh Dahal
Suresh Dahal am 10 Aug. 2017
Kommentiert: Jan am 10 Aug. 2017
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 Kommentare
KSSV
KSSV am 10 Aug. 2017
It is working fine right????_y_ is replaced by '_'....what you expect?
Suresh Dahal
Suresh Dahal am 10 Aug. 2017
Bearbeitet: Suresh Dahal am 10 Aug. 2017
yes but when I try to replace whole word 'skyyiy' by newStr=strrep(B,B,'-') it shows only '-' not '------', why's that I am not sure. I want to replace whole word with ---- at on instance.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

KL
KL am 10 Aug. 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 =
'------'

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 10 Aug. 2017
regexprep(B, c, '-')
or
B(B==c) = '_';
  3 Kommentare
Walter Roberson
Walter Roberson am 10 Aug. 2017
Bearbeitet: Jan am 10 Aug. 2017
Jan
Jan am 10 Aug. 2017
+1 for the last comment.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by