viewpoint-particle

Author Topic: StrReplace  (Read 507 times)

bettman

  • Newbie
  • *
  • Posts: 41
StrReplace
« 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.

jmburton2001

  • Jr. Member
  • **
  • Posts: 78
Re: StrReplace
« Reply #1 on: May 28, 2019, 12:17:20 pm »
I search strings using StrIndexNc so I can ignore the case.

bettman

  • Newbie
  • *
  • Posts: 41
Re: StrReplace
« Reply #2 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.

jmburton2001

  • Jr. Member
  • **
  • Posts: 78
Re: StrReplace
« Reply #3 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?

td

  • Tech Support
  • *****
  • Posts: 3453
    • WinBatch
Re: StrReplace
« Reply #4 on: May 28, 2019, 01:41:58 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
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