필터 지우기
필터 지우기

String Replacer only replaces one set of values and does not maintian previous changes

조회 수: 1 (최근 30일)
I have a String of words EX " the Big and Bad Dog" and need to replace 2 words not next to each other (Big and DOG). I split the string up by words and then coded the string replacer to replace these words. This worked but now the 1st word comes back after being replaced already

답변 (3개)

Steven Lord
Steven Lord 2018년 1월 10일
I can't be certain without seeing your code, but my guess is you did something like this:
oldString = "the Big and Bad Dog";
newString = replace(oldString, "Big", "Small")
newString = replace(oldString, "Dog", "Cat")
The problem is that the third line replaces "Dog" in oldString not the newString created on the second line. You could fix this by changing the third line:
oldString = "the Big and Bad Dog";
newString = replace(oldString, "Big", "Small")
newString = replace(newString, "Dog", "Cat")
Another alternative is to make both changes in one line.
newString = replace(oldString, ["Big", "Dog"], ["Small", "Cat"])

Walter Roberson
Walter Roberson 2018년 1월 10일
S = 'the Big and Bad Dog';
regexrep(S, {'Big', 'Dog'}, {'Small', 'Pinata'})

Matt Brianik
Matt Brianik 2018년 1월 10일
Thank you, this works

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by