digitalmars.D.learn - Does D have an equivalent to C#'s String.IsNullOrWhiteSpace?
- Nieto (5/5) Oct 12 2017 Does D have an equivalent to C#'s String.IsNullOrWhiteSpace() in
- Adam D. Ruppe (5/7) Oct 12 2017 import std.string;
- Jesse Phillips (4/11) Oct 12 2017 Also
- bauss (5/12) Oct 13 2017 Or this:
- Jonathan M Davis (57/73) Oct 13 2017 Nope. That's wrong. You should basically never test a string (or any typ...
- Jonathan M Davis (21/96) Oct 13 2017 Oh drat. I did this too quickly and wrote some of this backwards.
- Daniel Kozak (5/10) Oct 12 2017 Wow, C# is really wierd. They have method IsNullOrEmpty (OK why not), bu...
- Jacob Carlborg (5/8) Oct 12 2017 It's pretty neat functionality. Ruby has it (or rather Rails), it's
- Daniel Kozak (3/12) Oct 13 2017 Yes I beleive it is neat functionality. I just dont like those names :)
the standard library? just asking if there's a native one, so I don't need reinvente the wheel https://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
Oct 12 2017
On Thursday, 12 October 2017 at 18:11:55 UTC, Nieto wrote:in the standard library?import std.string; if(str.strip().length == 0) { // is null, empty, or all whitespace }
Oct 12 2017
On Thursday, 12 October 2017 at 18:17:54 UTC, Adam D. Ruppe wrote:On Thursday, 12 October 2017 at 18:11:55 UTC, Nieto wrote:Also if(str.strip().empty) std.array.empty() works essentially the same as IsNullOrEmpty()in the standard library?import std.string; if(str.strip().length == 0) { // is null, empty, or all whitespace }
Oct 12 2017
On Thursday, 12 October 2017 at 18:17:54 UTC, Adam D. Ruppe wrote:On Thursday, 12 October 2017 at 18:11:55 UTC, Nieto wrote:Or this: if(!str.strip()) { // is null, empty, or all whitespace }in the standard library?import std.string; if(str.strip().length == 0) { // is null, empty, or all whitespace }
Oct 13 2017
On Friday, October 13, 2017 07:36:28 bauss via Digitalmars-d-learn wrote:On Thursday, 12 October 2017 at 18:17:54 UTC, Adam D. Ruppe wrote:Nope. That's wrong. You should basically never test a string (or any type of dynamic array) with an if statement, loop condition, or assertion. It doesn't check whether the array is empty. It checks whether it's null. So, for instance, this code ------------------------- import std.stdio; import std.string; void main() { string str1 = "hello"; writeln(isNullOrEmptyOrWS("hello")); writeln(isNullOrEmptyOrWS(" ")); writeln(isNullOrEmptyOrWS("")); writeln(isNullOrEmptyOrWS(null)); } bool isNullOrEmptyOrWS(string str) { if(!str.strip()) return true; return false; } ------------------------- prints out false false false true whereas what you'd want is false true true true Putting anything in an if condition is an implicit, explict cast to bool (it's lowered to an explicit cast by the compiler, so its semantics are that of an explicit cast, but it's implicit in that the compiler does it for you). if(cond) is lowered to if(cast(bool)cond) and if(!cond) is lowered to if(!cast(bool)cond) and casting any dynamic array to a bool is equivalent to arr !is null. So, if(!str.strip()) becomes if(!cast(bool)(str.strip())) which becomes if(!(str.strip() is null)) which is the same as if(str.strip() !is null) whereas what you really want is if(str.strip().empty) or if(str.strip().length != 0) - Jonathan M DavisOn Thursday, 12 October 2017 at 18:11:55 UTC, Nieto wrote:Or this: if(!str.strip()) { // is null, empty, or all whitespace }in the standard library?import std.string; if(str.strip().length == 0) { // is null, empty, or all whitespace }
Oct 13 2017
On Friday, October 13, 2017 02:12:21 Jonathan M Davis via Digitalmars-d- learn wrote:On Friday, October 13, 2017 07:36:28 bauss via Digitalmars-d-learn wrote:Oh drat. I did this too quickly and wrote some of this backwards.On Thursday, 12 October 2017 at 18:17:54 UTC, Adam D. Ruppe wrote:Nope. That's wrong. You should basically never test a string (or any type of dynamic array) with an if statement, loop condition, or assertion. It doesn't check whether the array is empty. It checks whether it's null. So, for instance, this code ------------------------- import std.stdio; import std.string; void main() { string str1 = "hello"; writeln(isNullOrEmptyOrWS("hello")); writeln(isNullOrEmptyOrWS(" ")); writeln(isNullOrEmptyOrWS("")); writeln(isNullOrEmptyOrWS(null)); } bool isNullOrEmptyOrWS(string str) { if(!str.strip()) return true; return false; } ------------------------- prints out false false false true whereas what you'd want is false true true true Putting anything in an if condition is an implicit, explict cast to bool (it's lowered to an explicit cast by the compiler, so its semantics are that of an explicit cast, but it's implicit in that the compiler does it for you). if(cond) is lowered to if(cast(bool)cond) and if(!cond) is lowered to if(!cast(bool)cond) and casting any dynamic array to a bool is equivalent to arr !is null. So,On Thursday, 12 October 2017 at 18:11:55 UTC, Nieto wrote:Or this: if(!str.strip()) { // is null, empty, or all whitespace }in the standard library?import std.string; if(str.strip().length == 0) { // is null, empty, or all whitespace }if(!str.strip()) becomes if(!cast(bool)(str.strip())) which becomes if(!(str.strip() is null))Actually, it becomes if(!(str.strip() !is null)) because cast(bool)arr is results in true if the array is _not_ null.which is the same as if(str.strip() !is null)So, this is really if(str.strip() is null)whereas what you really want is if(str.strip().empty) or if(str.strip().length != 0)Hopefully, I got my point across in spite of screwing up some of the details in the lowerings. Just don't test dynamic arrays for whether they're true or not, because it's rarely what you want - and when it is what you want, anyone reading your code won't have any way of knowing whether you're purposefully checking for non-null or whether you just misunderstood and thought that you were checking for non-empty (and the odds are generally much higher that the person who wrote the code misunderstood). So, even if you know what it does, I'd argue that it's bad practice to do it. If you want to check for null, then do it explicitly, and if you want to check for empty, then do it explicitly. Then there's no ambiguity. - Jonathan M Davis
Oct 13 2017
they have IsNullOrWhiteSpace OK little akward but still OK until you realized it is more like IsNullOrEmptyOrWhiteSpace :D On Thu, Oct 12, 2017 at 8:11 PM, Nieto via Digitalmars-d-learn < digitalmars-d-learn puremagic.com> wrote:standard library? just asking if there's a native one, so I don't need reinvente the wheel https://msdn.microsoft.com/en-us/library/system.string.isnul lorwhitespace%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
Oct 12 2017
On 2017-10-12 21:42, Daniel Kozak wrote:but they have IsNullOrWhiteSpace OK little akward but still OK until you realized it is more like IsNullOrEmptyOrWhiteSpace :DIt's pretty neat functionality. Ruby has it (or rather Rails), it's called "blank?". -- /Jacob Carlborg
Oct 12 2017
Yes I beleive it is neat functionality. I just dont like those names :) On Fri, Oct 13, 2017 at 8:36 AM, Jacob Carlborg via Digitalmars-d-learn < digitalmars-d-learn puremagic.com> wrote:On 2017-10-12 21:42, Daniel Kozak wrote:they have IsNullOrWhiteSpace OK little akward but still OK until you realized it is more like IsNullOrEmptyOrWhiteSpace :DIt's pretty neat functionality. Ruby has it (or rather Rails), it's called "blank?". -- /Jacob Carlborg
Oct 13 2017