StrReplace

Started by bettman, May 28, 2019, 11:51:30 AM

Previous topic - Next topic

bettman

Could someone suggest an alternative to StrReplace that ignores case? I have a value that I'm reading into a variable from an ini file. The value will always be the same but the case may be different. Instead of using StrReplace against all of the different permutations of the value, I would like to just read it in once and then replace it. I thought about using strupper/strlower but converting the variable prior to searching doesn't always work with the values being read in.

Any help would be appreciated.

jmburton2001

I search strings using StrIndexNc so I can ignore the case.

bettman

Thank you for the reply.

The problem is, I need to replace the string if found. Since StrReplace requires the searched for string to be exact, I still have to replace it regardless of case.

jmburton2001

Can you give an example?

My impression is that the strings you need to replace are exactly the same except they may have case differences?

ThisIsAString
thISisaSTRING
THISisastrInG
thisisastring

If that's the case, what do you need to replace those strings with?

td

Quote from: bettman on May 28, 2019, 12:26:10 PM
Thank you for the reply.

The problem is, I need to replace the string if found. Since StrReplace requires the searched for string to be exact, I still have to replace it regardless of case.

You could do something like the following:

Code (winbatch) Select
new = "Joe"
old = "JOHN"
str = "Hello my name is john smith"

loc = StrIndexNc(str, old, 1, @fwdscan)
len = StrLen(old)
ret = StrSub(str, 1, loc-1):new:StrSub(str,loc+len, -1)
Message( "StrReplaceNC() Example", ret )

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade