SPONSORS:






User Tag List

Thanks Thanks:  0
Likes Likes:  0
Dislikes Dislikes:  0
Results 1 to 7 of 7
  1. #1
    Junior Member
    Join Date
    Feb 2005
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    How to convert a string to boolean

    I want to convert a string value to a boolean one. Could you please give an advice how to make this conversion?

    String str = "true"
    boolean b = str

    Thank you in advance!

  2. #2
    Senior Member
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    291
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: How to convert a string to boolean

    Assuming that the string is always going to be "true" or "false" you can just use a simple if statement.

    </font><blockquote><font size="1" face="Verdana, Arial, Helvetica">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;"> [ ]
    [ ] STRING s = &quot;True&quot;
    [ ] boolean b
    [ ]
    [ ] // convert to all lower-case first
    [ ] s = lower(s)
    [ ]
    [-] if(s == &quot;true&quot
    [ ] b = true
    [-] else
    [ ] b = false
    [ ] </pre><hr /></blockquote><font size="2" face="Verdana, Arial, Helvetica">That should do it. If this is something you need to do often you could stick it in its own method.

    Brendan

  3. #3
    Senior Member
    Join Date
    Jul 2001
    Location
    Elizabeth, CO, USA
    Posts
    653
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: How to convert a string to boolean

    Now, if you REALLY wanna be snazzy, you can do the following:

    </font><blockquote><font size="1" face="Verdana, Arial, Helvetica">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">[ ] STRING s = &quot;true&quot;
    [ ] BOOLEAN b
    [ ]
    [ ] b = Left(Lower(s), 1)==&quot;t&quot;?TRUE:FALSE
    [ ]
    [ ] Print (b)</pre><hr /></blockquote><font size="2" face="Verdana, Arial, Helvetica">That'll check to see if the lower, left-most letter is a t and return a TRUE if it is. Pretty much the same thing Brendan wrote above, but all in one line.

    - Dave

    P.S.- Special thanks to Jon Lamprech for reminding me of the syntax of this call.
    WaltzingRhino.com
    A concerned Borland customer, a fly in the ointment, a wrench in the works.

  4. #4
    Senior Member
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    291
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: How to convert a string to boolean

    Hell, you can even make that shorter...

    </font><blockquote><font size="1" face="Verdana, Arial, Helvetica">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">[ ] STRING s = &quot;true&quot;
    [ ] BOOLEAN b
    [ ]
    [ ] b = Left(Lower(s), 1)==&quot;t&quot;
    [ ]
    [ ] Print (b)</pre><hr /></blockquote><font size="2" face="Verdana, Arial, Helvetica">The ?TRUE:FALSE part is really redundant since you're saying if its TRUE return TRUE. You have to watch it with this notation because it can get really confusing at times.

    B

  5. #5
    Senior Member
    Join Date
    Jul 2001
    Location
    Elizabeth, CO, USA
    Posts
    653
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: How to convert a string to boolean

    True. Um...right. Whatever.

    And you are right. As such, I tend toward less complicated code when in doubt because I know that someday it may be someone else reading it. Hell, it may be me reading it and scratching my head about it. As such, I tend toward "a = a + 1" rather than "a++".

    - Dave
    WaltzingRhino.com
    A concerned Borland customer, a fly in the ointment, a wrench in the works.

  6. #6
    Junior Member
    Join Date
    Feb 2005
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: How to convert a string to boolean

    Thanks to ALL!
    As I see now, SilkTest doesn't make such conversation itself. I like this syntax:
    b = Left(Lower(s), 1)=="t"

    - Anna

  7. #7
    Advanced Member tonymro's Avatar
    Join Date
    Jul 1999
    Location
    Deep in the Coast Range Mountains of Oregon, USA
    Posts
    562
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: How to convert a string to boolean

    My $0.02...

    </font><blockquote><font size="1" face="Verdana, Arial, Helvetica">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">[-] boolean StrToBool(STRING sVal)
    [ ] // ============================================
    [ ] // Function: StrToBool()
    [ ] //
    [ ] // Description:
    [ ] // This function converts the string in sVal to a boolean result.
    [ ] // Valid string values for a boolean result of TRUE are TRUE, YES, Y, or 1.
    [ ] // Valid string values for a boolean result of FALSE are FALSE, NO, N, or 0.
    [ ] //
    [ ] // Parameters: STRING sVal the string to convert
    [ ] // Returns: BOOLEAN TRUE or FALSE
    [ ] // ============================================
    [ ] BOOLEAN bRetVal
    [ ] // Return True, False as appropriate. If returned value is neither, report an
    [ ] // error and return false.
    [-] switch (Upper(sVal))
    [-] case &quot;TRUE&quot;, &quot;YES&quot;, &quot;Y&quot;, &quot;1&quot;
    [ ] bRetVal = TRUE
    [-] case &quot;FALSE&quot;, &quot;NO&quot;, &quot;N&quot;, &quot;0&quot;
    [ ] bRetVal = FALSE
    [-] default
    [ ] bRetVal = FALSE
    [ ] Log.Error(&quot;StrToBool: Expected a boolean value. sVal contains '{sVal}'. Assigning {bRetVal}&quot
    [ ] return (bRetVal)</pre><hr /></blockquote><font size="2" face="Verdana, Arial, Helvetica">
    Sr. Software Test Automation Architect

 

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Resources saved on this page: MySQL 10.00%
vBulletin Optimisation provided by vB Optimise v2.6.4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.2.8 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominate (Lite) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 03:38 AM.

Copyright BetaSoft Inc.