Got error : return an empty string to bool
Make the empty check first
func beginsWithVowel(x: String) -> Bool {
if x.isEmpty { return false }
if x.characters[x.startIndex] == "a" { ...
Slightly different approach using regular expression, it includes the empty check:
func beginsWithVowel(x: String) -> Bool {
let regex = try! NSRegularExpression(pattern: "^[aeiou]", options: .caseInsensitive)
return regex.firstMatch(in: x, range: NSMakeRange(0, x.utf16.count)) != nil
}
In javascript, is an empty string always false as a boolean?
Yes. Javascript is a dialect of ECMAScript, and ECMAScript language specification clearly defines this behavior:
ToBoolean
The result is false if the argument is the empty String (its length is zero);
otherwise the result is true
Quote taken from http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
Converting from a string to boolean in Python
Really, you just compare the string to whatever you expect to accept as representing true, so you can do this:
s == 'True'
Or to checks against a whole bunch of values:
s.lower() in ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh']
Be cautious when using the following:
>>> bool("foo")
True
>>> bool("")
False
Empty strings evaluate to False
, but everything else evaluates to True
. So this should not be used for any kind of parsing purposes.
c# default value for Convert.ToBoolean
This is slightly diffrent logic, but this will give you false
for any value that does not correctly convert in to a Boolean which is likely what you are really looking for.
string temp = "";
bool result
if(!bool.TryParse(temp, out result))
{
result = false; //This line and the `if` is not actually necessary,
//result will be false if the parse fails.
}
SilentUpdate = result;
How do I check for an empty/undefined/null string in JavaScript?
Empty string, undefined, null, ...
To check for a truthy value:
if (strValue) {
// strValue was non-empty string, true, 42, Infinity, [], ...
}
To check for a falsy value:
if (!strValue) {
// strValue was empty string, false, 0, null, undefined, ...
}
Empty string (only!)
To check for exactly an empty string, compare for strict equality against ""
using the ===
operator:
if (strValue === "") {
// strValue was empty string
}
To check for not an empty string strictly, use the !==
operator:
if (strValue !== "") {
// strValue was not an empty string
}
Convert string to boolean in C#
I know this is not an ideal question to answer but as the OP seems to be a beginner, I'd love to share some basic knowledge with him... Hope everybody understands
OP, you can convert a string to type Boolean
by using any of the methods stated below:
string sample = "True";
bool myBool = bool.Parse(sample);
// Or
bool myBool = Convert.ToBoolean(sample);
bool.Parse
expects one parameter which in this case is sample
, .ToBoolean
also expects one parameter.
You can use TryParse
which is the same as Parse
but it doesn't throw any exception :)
string sample = "false";
Boolean myBool;
if (Boolean.TryParse(sample , out myBool))
{
// Do Something
}
Please note that you cannot convert any type of string to type Boolean
because the value of a Boolean
can only be True
or False
Hope you understand :)
How do I convert/cast a string into a bool in Tcl?
After thinking some more, and looking some more, the answer probably should be: "It depends"
With my perl background I was expecting something similar to perl. My fault.
In Tcl string is true
seems to be the official cast function. Valid strings for casting are defined here:
If string is any of 0, false, no, or off, then Tcl_GetBoolean stores a
zero value at *boolPtr. If string is any of 1, true, yes, or on, then
1 is stored at *boolPtr. Any of these values may be abbreviated, and
upper-case spellings are also acceptable
In my case, I don't care about the string's content so I can just
if {[string length [some_func $some_args]] > 0} {
...
}
Another way to write this also is much simpler:
if {[some_func $some_args] != {}} {
...
}
Related Topics
Pass Data from Tableviewcontroller to Another Tableviewcontroller in Swift
Could Not Find an Overload for '-' That Accepts The Supplied Arguments
Osx/Swift: Call a Function When Internet Connection Is Established/Restored
Swiching Between 2 Diferent Nsviewcontrollers with Data
Updating Existing Constraints Does Not Work, Wrong Use of .Active Property
Uisearchcontroller Hiding Status Bar on iOS 11 Swift 4
How to Remove The Fading Animation on .Ondelete Swiftui
Understanding UIviewrepresentable
Wkwebview Problems in Macos Mojave
Validatemenuitem or Menuwillopen Not Called for Nsmenu
Macos Remote Push Notifications Not Showing Alert Banner
Create Skspritenode with an Asset Programmatically
Is There an Object Class in Swift
How to Pass Data Backwards to a View Controller After Passing Forward
Swift Protocol That Is Using an Enum with Generic Associated Type