필터 지우기
필터 지우기

How do I replace a word in a string with ****?

조회 수: 5 (최근 30일)
Kratos
Kratos 2015년 2월 28일
편집: per isakson 2015년 3월 1일
Hello I have a text file where I am supposed to compare the strings in the text file with the words in another text file. After finding the word that I want to replace, how do I replace it? The first thing I did was use strfind to find the word. After that I have to replace the word with **?
str = 'Long day';
word = 'Long';
t = strfind(str, word);
After doing this I get 1. Now how do I change 'Long day' to '**** day'? The word can be of any length, so you may need more ********.
Thank you

채택된 답변

per isakson
per isakson 2015년 2월 28일
편집: per isakson 2015년 2월 28일
Try
str = strrep( str, 'Long', '****' );
  댓글 수: 2
Kratos
Kratos 2015년 2월 28일
I tried this but the thing is the word that I have to replace can be of any length. It could also be 'something' or 'MATLAB' anything.
per isakson
per isakson 2015년 3월 1일
편집: per isakson 2015년 3월 1일
"can be of any length" &nbsp strrep handles any length of the search and replacement string, respectively. That shouldn't be a problem.
>> str = strrep( 'abcd', 'c', '123456789' )
str =
ab123456789d
"It could also be 'something' or 'MATLAB' anything" &nbsp I guess you need to use string variables instead of static strings.
>> str = 'abcd';
>> s1 = 'c';
>> s2 = '123456789';
>> str = strrep( str, s1, s2 )
str =
ab123456789d
Then there is regexprep, but I don't think you need to use that for your current problem
"I tried this" &nbsp It is neither possible for me to know what you tried nor why you think it was no good.

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

추가 답변 (0개)

카테고리

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