www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Does D have an equivalent to C#'s String.IsNullOrWhiteSpace?

reply Nieto <neto.ribeiro email.com> writes:
Does D have an equivalent to C#'s String.IsNullOrWhiteSpace() in 
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
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 12 October 2017 at 18:11:55 UTC, Nieto wrote:
 Does D have an equivalent to C#'s String.IsNullOrWhiteSpace() 
 in the standard library?
import std.string; if(str.strip().length == 0) { // is null, empty, or all whitespace }
Oct 12
next sibling parent Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
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:
 Does D have an equivalent to C#'s String.IsNullOrWhiteSpace() 
 in the standard library?
import std.string; if(str.strip().length == 0) { // is null, empty, or all whitespace }
Also if(str.strip().empty) std.array.empty() works essentially the same as IsNullOrEmpty()
Oct 12
prev sibling parent reply bauss <jj_1337 live.dk> writes:
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:
 Does D have an equivalent to C#'s String.IsNullOrWhiteSpace() 
 in the standard library?
import std.string; if(str.strip().length == 0) { // is null, empty, or all whitespace }
Or this: if(!str.strip()) { // is null, empty, or all whitespace }
Oct 13
next sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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:
 On Thursday, 12 October 2017 at 18:11:55 UTC, Nieto wrote:
 Does D have an equivalent to C#'s String.IsNullOrWhiteSpace()
 in the standard library?
import std.string; if(str.strip().length == 0) { // is null, empty, or all whitespace }
Or this: if(!str.strip()) { // is null, empty, or all whitespace }
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 Davis
Oct 13
prev sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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:
 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:
 Does D have an equivalent to C#'s String.IsNullOrWhiteSpace()
 in the standard library?
import std.string; if(str.strip().length == 0) { // is null, empty, or all whitespace }
Or this: if(!str.strip()) { // is null, empty, or all whitespace }
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,
Oh drat. I did this too quickly and wrote some of this backwards.
 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
prev sibling parent reply Daniel Kozak <kozzi11 gmail.com> writes:
Wow, C# is really wierd. They have method IsNullOrEmpty (OK why not), but
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:

 Does D have an equivalent to C#'s String.IsNullOrWhiteSpace() in 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.isnul
 lorwhitespace%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
Oct 12
parent reply Jacob Carlborg <doob me.com> writes:
On 2017-10-12 21:42, Daniel Kozak wrote:
 Wow, C# is really wierd. They have method IsNullOrEmpty (OK why not), 
 but they have IsNullOrWhiteSpace OK little akward but still OK until you 
 realized it is more like IsNullOrEmptyOrWhiteSpace :D
It's pretty neat functionality. Ruby has it (or rather Rails), it's called "blank?". -- /Jacob Carlborg
Oct 12
parent Daniel Kozak <kozzi11 gmail.com> writes:
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:

 Wow, C# is really wierd. They have method IsNullOrEmpty (OK why not), but
 they have IsNullOrWhiteSpace OK little akward but still OK until you
 realized it is more like IsNullOrEmptyOrWhiteSpace :D
It's pretty neat functionality. Ruby has it (or rather Rails), it's called "blank?". -- /Jacob Carlborg
Oct 13