Recent

Author Topic: [SOLVED] IsNullOrWhiteSpace()  (Read 6376 times)

GypsyPrince

  • Guest
[SOLVED] IsNullOrWhiteSpace()
« on: March 24, 2020, 06:52:56 am »
Can someone please give me a brief but precise example of how to use the IsNullOrEmpty() function within a procedure or function?

This is basically what I'm trying to achieve:

Code: Pascal  [Select][+][-]
  1. If IsNullOrWhiteSpace(Expression):= False Then //If the expression contains valid data...
  2.     ShowMessage('String is legitimate.')
  3. Else //If the expression does not contain valid data...
  4.      ShowMessage('String is bad!')
  5. End If

This should be elementary and simple but I just can't get it to work.  The only documentation I can find for the function just shows its declaration in the TStringHelper class, which of course, is useless to me at the moment.

Code: Pascal  [Select][+][-]
  1. class function TStringHelper.IsNullOrEmpty(const AValue: string): Boolean;
  2. begin
  3.   Result:=system.Length(AValue)=0;
  4. end;
  5.  
  6. class function TStringHelper.IsNullOrWhiteSpace(const AValue: string): Boolean;
  7. begin
  8.   Result:=system.Length(SysUtils.Trim(AValue))=0;
  9. end;

Is there any documentation anywhere which shows basic examples of how to use each function in Object Pascal?  I remember in the 1990s we could get books 2" to 3" thick that were dedicated to actually explaining each function (divided by library/component) for C, C++, FoxPro, assembler, etc. 
« Last Edit: March 24, 2020, 06:16:31 pm by GypsyPrince »

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: IsNullOrEmpty()
« Reply #1 on: March 24, 2020, 07:01:26 am »
1) any helper becomes part of the type
2) you never use ":=" to check for equality just use "="
something along the lines of
Code: Pascal  [Select][+][-]
  1. If Expression.IsNullOrWhiteSpace Then //If the expression contains valid data...
  2.     ShowMessage('String is bad!')
  3. Else //If the expression does not contain valid data...
  4.      ShowMessage('String is legitimate.')
  5. End If

GypsyPrince

  • Guest
Re: IsNullOrWhiteSpace()
« Reply #2 on: March 24, 2020, 07:15:46 am »
Still not working.
I get a message saying "Error: Wrong number of parameters specified for call to "IsNullOrWhiteSpace"

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: IsNullOrWhiteSpace()
« Reply #3 on: March 24, 2020, 07:31:47 am »
Still not working.
I get a message saying "Error: Wrong number of parameters specified for call to "IsNullOrWhiteSpace"
report it.

GypsyPrince

  • Guest
Re: IsNullOrWhiteSpace()
« Reply #4 on: March 24, 2020, 08:16:56 am »
This is the only way I could get it to work.

Code: Pascal  [Select][+][-]
  1. If Expression.IsNullOrEmpty(Expression) = False Then //If the expression contains valid data...
  2.     ShowMessage('String is legitimate.')
  3. Else //If the expression does not contain valid data...
  4.     ShowMessage('String is bad!')

Even though this works at the moment, please tell me this is not the correct way to do it. Please tell me that whoever wrote this function in Free Pascal did not intend for the target string (Expression) to be utilized as both the type and the parameter,

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: IsNullOrWhiteSpace()
« Reply #5 on: March 24, 2020, 08:31:56 am »
I think that some one (probably overworked) did a quick job adding/converting those as a helper and did not pay attention to the design it self, otherwise there would be at least an overloaded method with out a parameter requirement as well.
But yes that the proper way to call it, you could try
Code: Pascal  [Select][+][-]
  1.   String.IsNullOrWhiteSpace(Expression)
  2.  
to avoid using the expression twice (for OCD programers).

GypsyPrince

  • Guest
Re: IsNullOrWhiteSpace()
« Reply #6 on: March 24, 2020, 08:38:15 am »
I just went ahead and created my own function called IsNullOrSpace()...

avra

  • Hero Member
  • *****
  • Posts: 2569
    • Additional info
Re: IsNullOrWhiteSpace()
« Reply #7 on: March 24, 2020, 09:07:10 am »
Is there any documentation anywhere which shows basic examples of how to use each function in Object Pascal?
http://delphibasics.co.uk/
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

GypsyPrince

  • Guest
Re: IsNullOrWhiteSpace()
« Reply #8 on: March 24, 2020, 09:29:39 am »
Thanks for that info, Avra. Unfortunately, the IsNullOrWhiteSpace() is not in there, or at least I could not find it using a logical means such as assuming it might be alphabetic or under functions. I wanted so much for Lazarus to be my new chosen development system, but I am finding so many inconsistencies with it, as well as so many things which just are not logical and a lack of structured documentation, I'm thinking I'm just going to set Lazarus to the side and continue with my current languages.
 
 
UPDATE:  I performed some more thorough searches and found that someone else had already discovered my same issue at this link: https://bugs.freepascal.org/view.php?id=33840

Apparently, the statement should be written in this following manner instead...

Code: Pascal  [Select][+][-]
  1. If ansistring.IsNullOrWhiteSpace(Expression) = False Then

But either way, I'm going to stick with my custom function to sidestep the bugginess.
« Last Edit: March 24, 2020, 09:48:53 am by GypsyPrince »

PascalDragon

  • Hero Member
  • *****
  • Posts: 6230
  • Compiler Developer
Re: IsNullOrWhiteSpace()
« Reply #9 on: March 24, 2020, 10:09:22 am »
Code: Pascal  [Select][+][-]
  1. If ansistring.IsNullOrWhiteSpace(Expression) = False Then

As a general remark: If you have an expression returning Boolean (like IsNullOrWhiteSpace does), then you should not use = False (or = True). Use the following instead:

Code: Pascal  [Select][+][-]
  1. if not AnsiString.IsNullOrWhiteSpace(Expression) then ...
  2. // or (for true)
  3. if AnsiString.IsNullOrWhiteSpace(Expression) then ...

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12563
  • FPC developer.
Re: IsNullOrWhiteSpace()
« Reply #10 on: March 24, 2020, 10:17:11 am »
Is there any documentation anywhere which shows basic examples of how to use each function in Object Pascal?  I remember in the 1990s we could get books 2" to 3" thick that were dedicated to actually explaining each function (divided by library/component) for C, C++, FoxPro, assembler, etc.

There is documentation, but examples are rare.

GypsyPrince

  • Guest
Re: IsNullOrWhiteSpace()
« Reply #11 on: March 24, 2020, 10:19:38 am »
@PascalDragon

Quote
As a general remark: If you have an expression returning Boolean (like IsNullOrWhiteSpace does), then you should not use = False (or = True). Use the following instead:

Code: Pascal  [Select]
if not AnsiString.IsNullOrWhiteSpace(Expression) then ...
// or (for true)
if AnsiString.IsNullOrWhiteSpace(Expression) then ...
What is your particular reasoning for this?

I prefer my verbosity. It leads to less buggy code.

jamie

  • Hero Member
  • *****
  • Posts: 7392
Re: [SOLVED] IsNullOrWhiteSpace()
« Reply #12 on: March 24, 2020, 09:50:56 pm »
a Null of course is an Empty string and a WhiteSpace is just a standard space.

 Not sure if the function accounts for multiple spaces grouped and wants to see only a single space?

Also you have the issue with a NonbreakingWhiteSpace, The value of which is 160, not 32..

the non breaking types are intended in some cases to prevent auto line wrapping and suppose use for other things. That too shows like a normal space so when viewing them you won't see the difference in a viewer..

 I found the best thing to do is simply first test for NULL, then Mask out the upper bit and then test for 32

 example

 Result :=  (Input = Nil)or((Input and $7F)=32;

 But then this only works if have a single character input.

 Obviously you need to loop through the whole string..

https://docs.microsoft.com/en-us/dotnet/api/system.string.isnullorwhitespace?view=netframework-4.8
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7392
Re: [SOLVED] IsNullOrWhiteSpace()
« Reply #13 on: March 24, 2020, 10:06:07 pm »
Something like this..
Code: Pascal  [Select][+][-]
  1. Function IsNullOrBothWhiteSpace(S:String):Boolean;
  2. Var
  3.   C:char;
  4. begin
  5.  result  := True;
  6.   For C in S do if Not (Ord(C) and $7F)=32 Then result := False;
  7. end;                                                              
  8.  
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7392
Re: [SOLVED] IsNullOrWhiteSpace()
« Reply #14 on: March 24, 2020, 11:00:51 pm »
Here's a more corrected code view to what MS states, this one handles the Non-Break and does the overload.
Code: Pascal  [Select][+][-]
  1. Function IsNullOrBothWhiteSpace(Const S:String):Boolean;
  2. Var
  3.   C:char;
  4. begin
  5.  result  := True;
  6.   For C in S do if Not ((Ord(C) and $7F)=32) Then result := False;
  7. end;
  8. Function IsNullOrBothWhiteSpace(const S:Pchar):Boolean;
  9. Begin
  10.  Result := IsNullOrBothWhiteSpace(String(S));
  11. end;                                          
  12.  

The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018