WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: bettman on May 28, 2019, 11:51:30 AM

Title: StrReplace
Post by: bettman on May 28, 2019, 11:51:30 AM
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.
Title: Re: StrReplace
Post by: jmburton2001 on May 28, 2019, 12:17:20 PM
I search strings using StrIndexNc so I can ignore the case.
Title: Re: StrReplace
Post by: 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.
Title: Re: StrReplace
Post by: jmburton2001 on May 28, 2019, 12:39:19 PM
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?
Title: Re: StrReplace
Post by: td on May 28, 2019, 01:41:58 PM
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 )