Skip to main content
Inspiring
November 28, 2008
Question

cfif and spaces

  • November 28, 2008
  • 4 replies
  • 740 views
I have a response code is a four string character e.g. YYYM or NNNN

sometimes it comes back with four spaces " " <<<no quotes of course.

If I am doing <cfif responseCode EQ " "> Will I catch it or not? Or will NEQ "" suffice?
    This topic has been closed for replies.

    4 replies

    Inspiring
    November 28, 2008
    > I have a response code is a four string character e.g. YYYM or NNNN
    >
    > sometimes it comes back with four spaces " " <<<no quotes of course.
    >
    > If I am doing <cfif responseCode EQ " "> Will I catch it or not? Or will
    > NEQ "" suffice?

    Over and above the other responses, you need to understand that what seems
    like blank space to you & I does not seem like blank space to a computer.
    The string " " (four space characters) is *absolutely* different from ""
    (a zero-length string).

    A space is a piece of data, represented (for our purposes) as a byte with
    value of 32 (decimal), 20 (hex) or 00100000 (binary). So at its most
    obtuse, a four-space string is represented by
    00100000001000000010000000100000 in memory. On the other hand, an empty
    string is represented by a pointer to a string variable with a null in it
    (I'm guessing Java still uses null-terminated strings?)

    So let's rephrase your question for clarity:

    Does 00100000001000000010000000100000 = 00000000? No. Clearly no.


    Sorry, this is an overwrought answer to your question, but sometimes it
    helps to stop and think about how the computer might see things, when
    wondering about the answer to some question that presents itself.


    --
    Adam
    November 28, 2008
    Yeah, actually, if you NEED an actual four character string then you could be even more strict. Try this instead:

    <cfif len(replace(responseCode," ","","all")) eq 4>
    do stuff here
    <cfelse>
    doesn't contain a string or is more or less than 4 characters long / short
    </cfif>

    I always format my strings when doing comparisons, despite how confident I think I know what it will return.

    What I do above, is:

    1 - Replace all white spaces from the variable using replace() function
    2 - Once this occurs, the len() function then tests the length against a value of 4

    Mikey.
    November 28, 2008
    Why don't you just do the following:

    <cfif trim(responseCode) EQ "">

    Using trim will remove these leading and ending white spaces. I think I've understood your problem but I could be wrong.

    Thanks,
    Mikey.
    Inspiring
    November 28, 2008
    What happened when you tried it?
    Inspiring
    November 28, 2008
    It is in production, but i am not sure it is doing anything.

    Mikey...I will give that a try it makes sense to blow away the white spaces and check to see if it is empty.

    Frank
    Inspiring
    November 28, 2008
    quote:

    Originally posted by: frank_tudor
    It is in production, but i am not sure it is doing anything.

    Mikey...I will give that a try it makes sense to blow away the white spaces and check to see if it is empty.

    Frank

    Testing 101.

    Step 1 - start a template that is not part of any application.
    Step 2 - write the code you want to test.
    Step 3 - run the page.