it is posible to convert a string into boolean like Convert.ToBoolean in c# ?
>if yes could you post the code please
C++ code help
Other urls found in this thread:
lmgtfy.com
lmgtfy.com
twitter.com
1mn bump
What's your use case? That sounds weird
You can test string if its "True" or "False" then return the bool acordingly, is that what you meant?
On my phone, and don't c++, but here is the shit way (don't use this for real):
Bool blah;
If (string == "true")
{
Blah = true;
}
Else if (string == "false)
{
Blah = false;
}
// Error check
If (null == blah)
Return;
Else
Do stuff
Who are you quoting?
Maybe something like this.
#include
#include
bool string_to_bool(std::string input, bool& result) {
std::transform(input.begin(), input.end(), input.begin(), ::tolower);
if (input == "true") {
result = true;
return true;
}
else if (input == "false"){
result = false;
return true;
}
else{
return false;
}
}
bool Convert_ToBoolean(string input)
{
return (string=="true"||string=="True"||string=="TRUE");
}
Surely?
return input == "true";
yeah but how do i check if "1>0" for example =true?
return (input==...)
My bad.
Convert.ToBoolean can't do that in the first place; you'd need to write a custom handler for such examples.
Sounds like some awful spaghetti code you're writing there, I'd reconsider whatever approach you're taking right now
I'm not sure what you are trying to accomplish. I'd say you need to use the regex std lib to parse the left hand value and right hand value and find what the operator is.
What about "TRue" and "trUe" and "tRuE" you pleb? Convert input to lowercase first PLEBIAN
You switch to some language other than sepples.
>>> eval("1>0")
True
CS grad meme tier shit. You should be ashamed
this
Boolean'Value(Your_String);
So... return input.Equals ("true", StringComparison.InvariantCultureIgnoreCase)
Dumbass
...
What about compiler which will turn all the examples above into same instructions you "non CS grad meme tier shit" clever guy?
Why have an out parameter as well as a return bool? Noone does that.
yes
For exception free code. Converting a string to bool have 3 possible outcomes. A bool has only 2 possible values.
This is the better way.
private static bool Convert2BooleanFunction( string input )
{
return ((( string[0] == 'T' || string[0] == 't' ) &&
( string[1] == 'R' || string[1] == 'r' ) &&
( string[2] == 'U' || string[2] == 'u' ) &&
( string[3] == 'E' || string[3] == 'e' )) ?
true :
((( string[0] == 'F' || string[0] == 'f' ) &&
( string[1] == 'A' || string[1] == 'a' ) &&
( string[2] == 'L' || string[2] == 'l' ) &&
( string[3] == 'S' || string[3] == 's' ) &&
( string[4] == 'E' || string[4] == 'e' )) ?
false :
( throw "Invalid string!" )));
}
>mfw java
return Boolean.parse(boolStr);
no need to worry about case
because exception-handling is a performance hit
you forgot to check that the string is only four characters long
First you have to define if leading and following spaces is valid.
I got you
fuck. here it is:
lmgtfy.com
...
they are still retarded
also who knows how well different compilers will optimize it.
Why are Sup Forumsfags so ugly?