Jump to content

Bug in StringReplace() already solved?


funkey
 Share

Recommended Posts

Hi,

 

I use v3.3.14.5 here and have a bug.

Local $sBug = StringReplace("123456789", 2, "a")
ConsoleWrite($sBug & " - replacements: " & @extended & " <-- expected 1" & @CRLF)

This is the result

1a3456789 - replacements: 0 <-- expected 1

 

Is this already solved in v3.3.16.1

I don't want to update now...

 

Thanks for your answers!

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

Don't know. Just found it.

Thanks for testing.

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

I'm not sure if this is by design or a bug. Since the function it's named StringReplace() the search string should be a string not an integer. If it's a string it works fine. Yet since AutoIt uses a variant implementation to represent variables and does all kind of conversions probably should deal internally and make sure the parameters are converted to strings.

The interesting part it's that in this code only the first 2 it's replaced with an a:

Local $sBug = StringReplace("1234567892", 2, "a")
ConsoleWrite($sBug & " - replacements: " & @extended & " <-- expected 1" & @CRLF)

and the result

Quote

1a34567892 - replacements: 0 <-- expected 2

I would like to know more about what AutoIt does internally but for now even if it's not enforced I would say it's up to programmer to be sure that the search string it's really a string.

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

I wanted it to be an integer for replacing characters at position. This is the second way StringReplace() works.

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

I don't need it. But the behaviour is not consistently. I just expected there to be a '1' in @extended. 

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

When you use it with value start which would be a number at a certain position it becomes vague to give input as number or as string

When you want to replace 1 char at a certain position result seems to be ok but @extended not filled with 1 (would be most logical)

;~ StringReplace ( "string", "searchstring/start", "replacestring" [, occurrence = 0 [, casesense = 0]] )

Local $sBug = StringReplace("001234567892", 2, "a")
ConsoleWrite($sBug & " - replacements: " & @extended & " <-- expected 1" & @CRLF)

;~  **** ambiguous ***
;~ unclear what to expect as input is string but could be interpreted as start
Local $sBug = StringReplace("001234567892", "2", "a")
ConsoleWrite($sBug & " - replacements: " & @extended & " <-- expected ?" & @CRLF)

 

Link to comment
Share on other sites

I suggest to replace the syntax description

StringReplace ( "string", "searchstring/start", "replacestring" [, occurrence = 0 [, casesense = 0]] )

with

StringReplace ( "string", "searchstring"/start, "replacestring" [, occurrence = 0 [, casesense = 0]] )

This makes clear that parameter 2 is either a string to search for or an integer to be used as the starting position.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

And it works by design🙂

If the start method is used the occurrence and casesense parameters are ignored. The function will replace the characters in "string", starting at the requested position, with the characters in "replacestring" - as many characters will be replaced as are in "replacestring". However, if there are not enough characters in "string" for the entire "replacestring" to be inserted an empty string is returned and @error is set to 1.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...