Modify

Opened 6 years ago

Closed 5 years ago

#2666 closed Bug (Fixed)

StringReplace($sString, $pos, "") doesn't work

Reported by: mikell Owned by: Melba23
Milestone: 3.3.13.14 Component: AutoIt
Version: 3.3.10.2 Severity: None
Keywords: Cc:

Description

StringReplace with "" (nothing) as replacement string doesn't work if using the character position

$sString = "abcde"

Msgbox(0,"", StringReplace($sString, "c", "") )   ; by searchstring : works

Msgbox(0,"", StringReplace($sString, 3, "#") )   ; by character position : works

Msgbox(0,"", StringReplace($sString, 3, "") )  ; by character position : doesn't work

Attachments (0)

Change History (11)

comment:1 Changed 6 years ago by mikell

Incidentally, if using "character position" StringReplace returns a wrong @extended value even when it's working

$sString = "abcde"

$res = StringReplace($sString, "c", "#") ; returns @extended = 1
Msgbox(0,"", $res & @crlf & @extended)

$res = StringReplace($sString, 3, "#")  ; returns @extended = 0 (expected 1)
Msgbox(0,"", $res & @crlf & @extended) 

comment:2 Changed 6 years ago by anonymous

Seems to me this is the expected behaviour. The function will start replacing from the start position (with the amount of characters of the replacement string). If the replacement string is empty, it will replace zero characters.

comment:3 Changed 6 years ago by mikell

I understand this logic
The confusion is that the way StringReplace operates is quite different in both cases (searchstring/start)

About @extended : if the start (character position) method is used then maybe storing in @extended the number of performed replacements makes no sense, as this number is equal to StringLen("replacestring")
Thus it's probably the reason why the @extended value is not set and always equal to 0 regardless of the number of characters replaced
If so, this should be a little better explained in the help file

comment:4 Changed 6 years ago by guinness

If this isn't a bug then additional remarks need to be added to StringReplace().

comment:5 Changed 6 years ago by AndyG

From the Helpfile:
"The substring to search for or the character position to start the replacement."

StringReplace($sString, 3, "#") ;works, the character at the 3rd position is replaced by "#"

StringReplace($sString, 3, "") ;doesn´t work, because the character at the 3rd position is not replaced by "nothing"

anonymous wrote:
"The function will start replacing from the start position" ;right, 3rd position is to be replaced

"(with the amount of characters of the replacement string)" ;right, amount is zero(-string) which replaces the string at this position

"If the replacement string is empty, it will replace zero characters." ;I think this is wrong. If the replacement string is empty, it has to be replaced with "nothing" (same as StringReplace((String,"d","") "d" is replaced with "nothing")

comment:6 Changed 6 years ago by mikell

Following anonymous' logic, the important concept in "character position to start the replacement" is the word "position"
Thus StringReplace($sString, 3, ...) doesn't mean "replace 3rd character", it means "start replacing from 3rd character position"
This looks a lot like some regex patterns allowing to match a position between 2 characters rather than a "real" string or character

comment:7 Changed 6 years ago by anonymous

If your replacement string would contain one character, it would replace position 3.
StringReplace("abcdef", 3, "X") returns "abXdef"

If your replacement string would contain two caracters, it would replace position 3 and 4.
StringReplace("abcdef", 3, "XY") returns "abXYef"

"It works like correction fluid for the length of the replacement string"
It will start replacing character for character form the starting position.
Therefore, if your replacement string has no characters, it will not replace anything.

You cannot replace one character with more/less than one character if a starting position is used.

Hopefully this explains enough.

comment:8 follow-up: Changed 6 years ago by anonymous

Ok, understandable.
That should be added in the help, just as explained.

"The confusion is that the way StringReplace operates is quite different in both cases (searchstring/start)"
That´s it....

comment:9 in reply to: ↑ 8 Changed 6 years ago by mlipok

Replying to guinness:

If this isn't a bug then additional remarks need to be added to StringReplace().

Replying to anonymous:

Ok, understandable.
That should be added in the help, just as explained.

"The confusion is that the way StringReplace operates is quite different in both cases (searchstring/start)"
That´s it....

I watched this Track Ticket notification
Change the documentation seems to be necessary.

comment:10 Changed 5 years ago by Melba23

Help file amended.

M23

comment:11 Changed 5 years ago by Melba23

  • Milestone set to 3.3.13.14
  • Owner set to Melba23
  • Resolution set to Fixed
  • Status changed from new to closed

Fixed by revision [10560] in version: 3.3.13.14

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The owner will remain Melba23.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.