PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 28, 2022

[FIXED] How to discriminate between null and zero length string in .NET?

 October 28, 2022     .net, is-empty, nothing, string, vb.net     No comments   

Issue

What is the most efficient way to discriminate between empty and null value? I want to:

  1. evaluate CStr(str) to True when str="", whereas
  2. evaluate CStr(str) to False when str=Nothing

Solution

Here it is:

<Runtime.CompilerServices.Extension>
Public Function HasValue(s As String)
Return TypeOf (s) Is String
End Function

Slightly Better equivalent: (from answer of jmcilhinney)

<Runtime.CompilerServices.Extension>
Public Function HasValue(s As String)
Return  s IsNot Nothing
End Function

Also a benchmark of various methods on 10000 strings of various length:

Function(x As String)..............:Total Time (rel Efficency %)

TypeName(x) = "String".....................:0.850ms (17.1%)

VarType(x) = VariantType.String........:0.590ms (24.6%)

TypeOf (x) Is String........................... :0.150ms (96.7%)

x IsNot Nothing................................. :0.145ms (100%)



Answered By - anefeletos
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing