필터 지우기
필터 지우기

Replace each characters of strings with '-'

조회 수: 2 (최근 30일)
Suresh Dahal
Suresh Dahal 2017년 8월 10일
댓글: Jan 2017년 8월 10일
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
KSSV
KSSV 2017년 8월 10일
It is working fine right????_y_ is replaced by '_'....what you expect?
Suresh Dahal
Suresh Dahal 2017년 8월 10일
편집: Suresh Dahal 2017년 8월 10일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

KL
KL 2017년 8월 10일
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 =
'------'

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 8월 10일
regexprep(B, c, '-')
or
B(B==c) = '_';
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 8월 10일
편집: Jan 2017년 8월 10일
Jan
Jan 2017년 8월 10일
+1 for the last comment.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by