www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: how to test if a string is not empty

  1. #1
    Join Date
    Jul 2003
    Location
    UK
    Posts
    392

    how to test if a string is not empty

    hi,

    how do you test if aa string is not empty in vbscript?



    thanks a lot

  2. #2
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Code:
    strName = "buntine"
    
    If strName <> "" Then
      ' String is not empty...
    End If
    Or you could use:
    Code:
    If Len(strName) Then
      ' String is not empty.
    End If
    In the latter example, the Len function will return a value greater than one, which is treated the same as true in an if statement.

    Regards.

  3. #3
    Join Date
    Dec 2004
    Posts
    8,637
    This would be considered a bad programming practice:
    Code:
    If Len(strName) Then
      ' String is not empty.
    End If
    The reason has to do with invalid use of the returned data subtype. Using it can lead a programmer to make other such mistakes that would lead to errors, etc. Instead, to maintain accurate use of data subtypes, you can achieve the same desired results -- and make the code more intuitive -- by coding it this way:
    Code:
    If Len(strName) > 0 Then
      ' String is not empty.
    End If

  4. #4
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Please backup your argument.

  5. #5
    Join Date
    Dec 2004
    Posts
    8,637
    I thought I did. To what are you referring?

    "Just because something works -- doesn't make it correct."

  6. #6
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Using it can lead a programmer to make other such mistakes that would lead to errors, etc.
    Examples?

    Anyone with a working knowledge of a modern programming language should be able to understand what this statement is doing and why it works. Otherwise, the functionality would not exist.

    Regards.

  7. #7
    Join Date
    Dec 2004
    Posts
    8,637
    Originally posted by buntine
    Examples?

    Anyone with a working knowledge of a modern programming language should be able to understand what this statement is doing and why it works. Otherwise, the functionality would not exist.
    Most programming language do not allow such ambiguity. What ambiguity? Numeric results where Boolean results would normally be expected. It takes more than just a "working knowledge" of VBScript to truly understand how such a mis-match in data subtypes is actually being handled. Examples?

    Programmer sees a statement like this:
    Code:
    If Len(str1) Then ...
    Programmer thinks, "The Len() function returns a numeric result which is treated as a Boolean condition." Programmer may then get the idea that positive numbers, including decimals, greater than zero are treated as True and zero or negative numbers are treated as False.

    This is not a good way to think. That is why I said it was a bad programming practice -- it is ambiguous to code that way.
    Last edited by phpnovice; 03-24-2005 at 11:40 AM.

  8. #8
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Most programming language do not allow such ambiguity. What ambiguity? Numeric results where Boolean results would normally be expected.
    What!? Which programming languages do you use? Every MS technology I am familiar with allows it. Java (and JSP) allows it. C allows it. C++ allows it. JavaScript allows it. PHP allows it. PL/SQL allows it. Perl allows it. Python allows it.

    You should not try to make yourself sound better than others. The least you could have said was "Here is the way I like to do it: ... This is advantageous because...".
    Programmer may then get the idea that positive numbers, including decimals, greater than zero are treated as True and zero or negative numbers are treated as False.
    In the conxext of an If Statment, this is true. Everyone should know that.

    Regards.
    Last edited by buntine; 03-24-2005 at 09:24 PM.

  9. #9
    Join Date
    Dec 2004
    Posts
    8,637
    Originally posted by buntine
    What!? Which programming languages do you use? Every MS technology I am familiar with allows it.
    Well, sorry, if that is true then I guess I have an advantage over you. I don't just program in the languages of the Internet or for PCs. I program in many languages for midrange and mainframe computers, too. NONE of those languages allow ambiguous use of data types. Period.
    Originally posted by buntine
    You should not try to make yourself sound better than others. The least you could have said was "Here is the way I like to do it: ... This is advantageous because...".
    You misjudge me. I think you chose to take personal insult where none was given. My statements are not only based on 25+ years of programming experience. If they were, then perhaps I would have said things in a way that would have eased your bruised ego. Instead, my statements are based upon what educators have drilled into my head over those same many years. Tell me, since I don't know you, are you actually trained in Information Science and Programming? ...or is programming something you have just "picked up" here and there? Do you also have a degree in the field?

    If so, I think your education has actually been lacking. Just because a programming environment allows a thing, does not mean it is something that should actually be exploited as correct usage. Also, don't hold Microsoft technology up as some kind of Holy Grail. After all, look at all the criticism and downright condemnation Microsoft has received for its efforts to make client-side technology as powerful as server-side technology. Case in point... In spite of everything that JavaScript, and particularly IE's JScript, can do; practically all you hear at this website is that it shouldn't be used. Those who try to use it are actually vilified and ostracized for it.

    No, thank you very much, but I'll stick to my guns. Purposely writing program code to be ambiguous is just plain wrong. This is especially true when absolutely no less functionality is given if such code is written with clear and obvious intent. Preferring the ambiguous over the obvious smacks of just plain laziness. Period.
    Originally posted by buntine
    Originally posted by phpnovice
    Programmer thinks, "The Len() function returns a numeric result which is treated as a Boolean condition." Programmer may then get the idea that positive numbers, including decimals, greater than zero are treated as True and zero or negative numbers are treated as False.
    In the conxext of an If Statment, this is true. Everyone should know that.
    See?!? Even you, who think so highly of yourself, don't know VBScript well enough to know what is true about the If statement where numeric results are involved. My quote said that a programmer might think negative numbers are treated as a false conditional. You agreed with that and you are wrong. Instead, negative numbers are treated as true conditionals. Only the zero is treated as a false conditional. You just proved my point. Thank you.

    I actually did say one thing incorrectly earlier, though.
    Originally posted by phpnovice
    The reason has to do with invalid use of the returned data subtype.
    I did not mean to say it that way -- though this is not the statement that was picked on. Instead, this is what I meant:

    "The reason has to do with ambiguous use of the returned data subtype."

    Cheers.
    Last edited by phpnovice; 03-24-2005 at 11:02 PM.

  10. #10
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    I'm going to stick my nose in here where it doesn't belong.

    I think there is value in both your arguments.

    Buntine is right in that, in many programming languages, false is represented as a 0 integer value, and all other integer values are equivalent to true. It is therefore not ambiguous (from the compiler's point of view) to treat an integer value as a boolean in such languages.

    However, phpnovice is right in that it is more cryptic and less readable, especially for for inexperienced programmers, and may lead to the misunderstanding that all languages work this way when they in fact do not.

    That's my two cents. Feel free to disagree...
    Last edited by HaganeNoKokoro; 03-24-2005 at 10:11 PM.
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  11. #11
    Join Date
    Dec 2004
    Posts
    8,637
    Originally posted by HaganeNoKokoro
    Buntine is right in that, in many programming languages, false is represented as a 0 integer value, and all other integer values are equivalent to true.
    The fact that this is supported by some programming languages (though never stated exactly as you have stated it), was never at issue. Plus, you also make my point because you limit this to "integer" when, in fact, single and double decimal values are also supported as being equivalent to "true".

    Load the following up in your IE browser and you'll see:
    Code:
    <body>
    <script language="vbscript">
    str1 = ""
    If +1.1 Then str1 = str1 & "+1.1 is true<br>" & vbCrLf
    If +1   Then str1 = str1 & "+1   is true<br>" & vbCrLf
    If +.1  Then str1 = str1 & "+.1  is true<br>" & vbCrLf
    If  0   Then str1 = str1 & " 0   is true<br>" & vbCrLf
    If -.1  Then str1 = str1 & "-.1  is true<br>" & vbCrLf
    If -1   Then str1 = str1 & "-1   is true<br>" & vbCrLf
    If -1.1 Then str1 = str1 & "-1.1 is true<br>" & vbCrLf
    Document.Write str1
    </script>
    </body>
    Last edited by phpnovice; 03-24-2005 at 11:00 PM.

  12. #12
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Content removed.
    Last edited by buntine; 03-26-2005 at 03:57 AM.

  13. #13
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Ok, so I should have said "numeric" instead of "integer". What I said is still true.
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  14. #14
    Join Date
    Dec 2004
    Posts
    8,637
    Quote Originally Posted by buntine
    In the future, do it in a more suitable way or I will take action. You have seen my example.
    That sounds like a threat. What "action"? There is no possible action you could take that would not be a violation and an abuse of your status on this site. Just making such a threat is a violation and an abuse of your status. I have violated no rules of this site. You had better retract that immediately -- or I'll take this thread to the site administration and let them deal with you. And, what "example"? The example of a dictator who will brook no opposition to his own point of view?

    I said nothing against you, personally, in my original comments. The fact that you have taken exception to them is inexcusable in light of your status on this site. Yes, you are entitled to your opinion, but so am I. What I said is not even just my opinion. It is a fact that is expressly taught in every programming class that I have ever attended -- and there have been many. You don't have to agree with such facts. But don't take it out on me because I point out these facts.

    Now... I have copied all of these posts to my PC as proof of what was said. I am ready to submit these to the site administration in justification of myself and in condemnation of your overbearing attitude. In your position, you are not allowed to demonstrate such an attitude unless a member is actually and willfully violating the rules of this site.

  15. #15
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    I have spoken to the administrators and fellow moderators about this issue. I will retract my statement (as you can see my threating statement is now gone). And I apologise to you for attempting to lay down the law upon you in an irresponsible way.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles