Modify

Opened 12 years ago

Closed 12 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 by mikell, 12 years ago

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 by anonymous, 12 years ago

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 by mikell, 12 years ago

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 by guinness, 12 years ago

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

comment:5 by AndyG, 12 years ago

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 by mikell, 12 years ago

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 by anonymous, 12 years ago

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 by anonymous, 12 years ago

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....

in reply to:  8 comment:9 by mlipok, 12 years ago

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 by Melba23, 12 years ago

Help file amended.

M23

comment:11 by Melba23, 12 years ago

Milestone: 3.3.13.14
Owner: set to Melba23
Resolution: Fixed
Status: newclosed

Fixed by revision [10560] in version: 3.3.13.14

Modify Ticket

Action
as closed The owner will remain Melba23.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.