it is posible to convert a string into boolean like Convert.ToBoolean in c# ?
>if yes could you post the code please
1mn bump
What's your use case? That sounds weird
>>57297637
You can test string if its "True" or "False" then return the bool acordingly, is that what you meant?
>>57297637
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
>>57297637
Who are you quoting?
>>57297637
Maybe something like this.#include <algorithm>
#include <string>
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?
>>57297637return input == "true";
>>57297980
yeah but how do i check if "1>0" for example =true?
>>57298037
return (input==...)
My bad.
>>57298045
Convert.ToBoolean can't do that in the first place; you'd need to write a custom handler for such examples.
>>57298045
Sounds like some awful spaghetti code you're writing there, I'd reconsider whatever approach you're taking right now
>>57298045
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.
>>57298037
What about "TRue" and "trUe" and "tRuE" you pleb? Convert input to lowercase first PLEBIAN
>>57298045
You switch to some language other than sepples.>>> eval("1>0")
True
>>57297946
>>57297917
>>57297980
>>57298037
CS grad meme tier shit. You should be ashamed
>>57298042
this
>>57297637
Boolean'Value(Your_String);
>>57298037
So... return input.Equals ("true", StringComparison.InvariantCultureIgnoreCase)
Dumbass
>>57300514
What about compiler which will turn all the examples above into same instructions you "non CS grad meme tier shit" clever guy?
>>57297980
Why have an out parameter as well as a return bool? Noone does that.
>>57297637
yes
>>57302014
For exception free code. Converting a string to bool have 3 possible outcomes. A bool has only 2 possible values.
>>57298037
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
>>57302014
because exception-handling is a performance hit
>>57302232
you forgot to check that the string is only four characters long
First you have to define if leading and following spaces is valid.
>>57297637
I got you
http://lmgtfy.com/?s=d&q=string+to+boolean
>>57302402
fuck. here it is:
http://lmgtfy.com/?q=string+to+boolean+c%2B%2B
>>57301957
they are still retarded
also who knows how well different compilers will optimize it.
>>57297637
Why are /g/fags so ugly?