Jump to content

StringManagement .... Bug in StringReplace() ????


Recommended Posts

 

I've just discovered that in StringReplace() -- if the SearchString is null, then the returned value will also be null.

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

Here's an example:

$var1 = "ABCDEFG"
$var2 = ""
$var3 = "X"

$var1 = StringReplace($var1, $var2, $var3)

$Msg    =        "var1 = "        & $var1     &  @CRLF
$Msg    = $Msg & "@error = "      & @error    &  @CRLF
$Msg    = $Msg & "@extended = "   & @error

MsgBox($MB_SYSTEMMODAL, "", $Msg)

The result will be:    $var = null, @error = 1, @extended = 1

According to the documentation @extended says how many replacements were made.
But it does not mention anything about @error.

Is this a bug -- or a special feature?

 

 

 

Link to comment
Share on other sites

Maybe a little buggy with empty strings as this shows 0 replacements.  I also believe it is returning an empty string (not NULL) and setting the error to 1 as the behavior for any/all errors.

$var1 = "ABCDEFG"
$var2 = "AB"
$var3 = ""

$var1 = StringReplace($var1, $var2, $var3)

$Msg    =        "var1 = "        & $var1     &  @CRLF
$Msg    = $Msg & "@error = "      & @error    &  @CRLF
$Msg    = $Msg & "@extended = "   & @error

MsgBox(0, "", $Msg)

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Thank you all for getting back to me.

Iamtheky writes:
> Maybe a little buggy with empty strings as this shows 0 replacements.  
> I also believe it is returning an empty string (not NULL) and setting the error to 1 as the behavior for any/all errors.

Are there two separate things??  An "empty" variable and a variable containing a "null" value (asci(0)).
The following script shows they are the same.

$var2 = Chr(0)
        $var3 = ""
        $var4 = ""

        If $var2 = $var3   Then
            $var4 = "They are equal"
        Else
            $var4 = "They are not equal"
        Endif

        $var5 = ASC($var2)
        $var6 = ASC($var3)
        $Msg =        "ASCI($var2) = "        & $var5   &  @CRLF
        $Msg = $Msg & "ASCI($var3) = "        & $var6   &  @CRLF
        $Msg = $Msg & $var4
        MsgBox($MB_SYSTEMMODAL, "", $Msg)

 

Andy writes:
> seems to be a never ending story...
> https://www.autoitscript.com/trac/autoit/ticket/2666

Thank you. Very interesting. However I don’t see anything in that post specific to my issue.
 

AutoBert writes:
> This is the result of your bugy script (c&p). When corrected output is:
> var1 =
> @error = 1
> @extended = 0  


Thank you. Yes my mistake (copy/pasting) regarding @extended.
Instead of:             $Msg    = $Msg & "@extended = "   & @error
It should read:       $Msg    = $Msg & "@extended = "   & @extended

CASE1
    $var1 = "ABCDEFG"
    $var2 = ""
    $var3 = "X"
    $var1 = StringReplace($var1, $var2, $var3)
The result will be:    $var1 = null, @error = 1, @extended = 0

Questions:  
(1) Since there were no replacements (@extended = 0) why is $var1 changed to Null.
(2) Why would there be a replacement? $var2 is not found in $var1.
(3) The fact that the documentation makes no mention of @error makes me wonder.
(4) I wonder if any other String functions handle null values poorly like this.
(5) Do you think I should report this as a bug? It would be a simple fix. At the beginning of the function check Parameter2 (the SearchString). If it is null -- Then Return.

 

Link to comment
Share on other sites

35 minutes ago, CE101 said:

Are there two separate things??

Dont know, I was more attempting to convey that i believe it only behaves as stated in the help file, it just does so for more cases.

 

Helpfile should read:

"However, if there are not enough characters in "string" for the entire "replacestring" to be inserted, *or any other stupid stuff with the strings*, an empty string is returned and @error is set to 1."

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

9 hours ago, CE101 said:

Are there two separate things??  An "empty" variable and a variable containing a "null" value (asci(0)).

An empty string (variable of type String having length = 0) is a completely distinct beast from a variable containing the keyword Null (see help file). Null is to be understood as "I don't know", i.e. denotes that a variable has no defined type and hence no defined value. From this point of view it's the same concept as Null in SQL, despite Null in AutoIt does convert differently from SQL in expressions.

A string containing the ASCII velue NULL is = Chr(0x00) so it's a string of length 1 containing the ASCII character Chr(0x00) aka NULL. Again a completely distinct thing.

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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