<p>Which is more efficient for the compiler and the best practice for checking whether a string is blank? </p>

<ol>
<li>Checking whether the length of the string == 0</li>
<li>Checking whether the string is empty (strVar == "")</li>
</ol>

<p>Also, does the answer depend on language?</p>

[{'answer_id': 10233, 'author': 'Stu', 'author_id': 414, 'author_profile': 'https://Stackoverflow.com/users/414', 'pm_score': 5, 'selected': True, 'text': '<p>Yes, it depends on language, since string storage differs between languages.</p>\n\n<ul>\n<li>Pascal-type strings: <code>Length = 0</code>.</li>\n<li>C-style strings: <code>[0] == 0</code>. </li>\n<li>.NET: <code>.IsNullOrEmpty</code>.</li>\n</ul>\n\n<p>Etc. </p>\n'}, {'answer_id': 10234, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 2, 'selected': False, 'text': '<p>In .Net:</p>\n\n<pre><code>string.IsNullOrEmpty( nystr );\n</code></pre>\n\n<p>strings can be null, so .Length sometimes throws a NullReferenceException</p>\n'}, {'answer_id': 10235, 'author': 'Vaibhav', 'author_id': 380, 'author_profile': 'https://Stackoverflow.com/users/380', 'pm_score': 0, 'selected': False, 'text': '<p>Actually, IMO the best way to determine is the IsNullOrEmpty() method of the string class.</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.string.isnullorempty." rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.</a></p>\n\n<p>Update: I assumed .Net, in other languages, this might be different.</p>\n'}, {'answer_id': 10250, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 4, 'selected': False, 'text': '<p>In languages that use C-style (null-terminated) strings, comparing to <code>""</code> will be faster.  That\'s an O(1) operation, while taking the length of a C-style string is O(n).</p>\n\n<p>In languages that store length as part of the string object (C#, Java, ...) checking the length is also O(1).  In this case, directly checking the length is faster, because it avoids the overhead of constructing the new empty string.</p>\n'}, {'answer_id': 10283, 'author': 'bpapa', 'author_id': 543, 'author_profile': 'https://Stackoverflow.com/users/543', 'pm_score': 1, 'selected': False, 'text': '<p>In Java 1.6, the String class has a new method [isEmpty] <a href="http://java.sun.com/javase/6/docs/api/java/lang/String.html#isEmpty()" rel="nofollow noreferrer">1</a></p>\n<p>There is also the Jakarta commons library, which has the [isBlank] <a href="http://web.archive.org/web/20100802010006/http://commons.apache.org:80/lang/api/org/apache/commons/lang/StringUtils.html" rel="nofollow noreferrer">2</a> method. Blank is defined as a string that contains only whitespace.</p>\n'}, {'answer_id': 10294, 'author': 'GaryF', 'author_id': 1035, 'author_profile': 'https://Stackoverflow.com/users/1035', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>In this case, directly checking the length is faster, because it avoids the overhead of constructing the new empty string.</p>\n</blockquote>\n\n<p>@DerekPark: That\'s not always true. "" is a string literal so, in Java, it will almost certainly already be interned.</p>\n'}, {'answer_id': 10305, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 1, 'selected': False, 'text': '<p>For C strings,</p>\n\n<pre><code>if (s[0] == 0)\n</code></pre>\n\n<p>will be faster than either</p>\n\n<pre><code>if (strlen(s) == 0)\n</code></pre>\n\n<p>or</p>\n\n<pre><code>if (strcmp(s, "") == 0)\n</code></pre>\n\n<p>because you will avoid the overhead of a function call.</p>\n'}, {'answer_id': 10306, 'author': 'Nathan Fellman', 'author_id': 1084, 'author_profile': 'https://Stackoverflow.com/users/1084', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>In languages that use C-style (null-terminated) strings, comparing to "" will be faster</p>\n</blockquote>\n\n<p>Actually, it may be better to check if the first char in the string is \'\\0\':</p>\n\n<pre><code>char *mystring;\n/* do something with the string */\nif ((mystring != NULL) &amp;&amp; (mystring[0] == \'\\0\')) {\n    /* the string is empty */\n}\n</code></pre>\n\n<p>In Perl there\'s a third option, that the string is undefined.  This is a bit different from a NULL pointer in C, if only because you don\'t get a segmentation fault for accessing an undefined string. </p>\n'}, {'answer_id': 10312, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 0, 'selected': False, 'text': "<p>@Nathan</p>\n\n<blockquote>\n  <p>Actually, it may be better to check if the first char in the string is '\\0':</p>\n</blockquote>\n\n<p>I almost mentioned that, but ended up leaving it out, since calling <code>strcmp()</code> with the empty string and directly checking the first character in the string are both O(1).  You basically just pay for an extra function call, which is pretty cheap.  If you <em>really</em> need the absolute best speed, though, definitely go with a direct first-char-to-0 comparison.</p>\n\n<p>Honestly, I always use <code>strlen() == 0</code>, because I have <em>never</em> written a program where this was actually a measurable performance issue, and I think that's the most readable way to express the check.</p>\n"}, {'answer_id': 10902, 'author': 'Surgical Coder', 'author_id': 1276, 'author_profile': 'https://Stackoverflow.com/users/1276', 'pm_score': 2, 'selected': False, 'text': '<p><code>String.IsNullOrEmpty()</code> only works on .net 2.0 and above, for .net 1/1.1, I tend to use:</p>\n\n<pre><code>if (inputString == null || inputString == String.Empty)\n{\n    // String is null or empty, do something clever here. Or just expload.\n}\n</code></pre>\n\n<p>I use String.Empty as opposed to "" because "" will create an object, whereas String.Empty wont - I know its something small and trivial, but id still rather not create objects when I dont need them! (<a href="http://blogs.msdn.com/brada/archive/2003/04/22/49997.aspx" rel="nofollow noreferrer">Source</a>)</p>\n'}, {'answer_id': 163570, 'author': 'Andy Lester', 'author_id': 8454, 'author_profile': 'https://Stackoverflow.com/users/8454', 'pm_score': 0, 'selected': False, 'text': "<p>Again, without knowing the language, it's impossible to tell.</p>\n\n<p>However, I recommend that you choose the technique that makes the most sense to the maintenance programmer that follows and will have to maintain your work.</p>\n\n<p>I'd recommend writing a function that explicitly does what you want, such as</p>\n\n<pre><code>#define IS_EMPTY(s) ((s)[0]==0)\n</code></pre>\n\n<p>or comparable.  Now there's no doubt at is you're checking.</p>\n"}, {'answer_id': 2306659, 'author': 'Shimmy Weitzhandler', 'author_id': 75500, 'author_profile': 'https://Stackoverflow.com/users/75500', 'pm_score': 1, 'selected': False, 'text': '<p>Assuming your question is .NET:</p>\n\n<p>If you want to validate your string against nullity as well use IsNullOrEmpty, if you know already that your string is not null, for example when checking TextBox.Text etc., do not use IsNullOrEmpty, and then comes in your question.<br>\nSo for my opinion String.Length is less perfomance than string comparison.</p>\n\n<p>I event tested it (I also tested with C#, same result):</p>\n\n<pre><code>Module Module1\n  Sub Main()\n    Dim myString = ""\n\n\n    Dim a, b, c, d As Long\n\n    Console.WriteLine("Way 1...")\n\n    a = Now.Ticks\n    For index = 0 To 10000000\n      Dim isEmpty = myString = ""\n    Next\n    b = Now.Ticks\n\n    Console.WriteLine("Way 2...")\n\n    c = Now.Ticks\n    For index = 0 To 10000000\n      Dim isEmpty = myString.Length = 0\n    Next\n    d = Now.Ticks\n\n    Dim way1 = b - a, way2 = d - c\n\n    Console.WriteLine("way 1 took {0} ticks", way1)\n    Console.WriteLine("way 2 took {0} ticks", way2)\n    Console.WriteLine("way 1 took {0} ticks more than way 2", way1 - way2)\n    Console.Read()\n  End Sub\nEnd Module\n</code></pre>\n\n<p>Result:</p>\n\n<pre><code>Way 1...\nWay 2...\nway 1 took 624001 ticks\nway 2 took 468001 ticks\nway 1 took 156000 ticks more than way 2\n</code></pre>\n\n<p>Which means comparison takes way more than string length check.</p>\n'}, {'answer_id': 34248826, 'author': 'David A. Gray', 'author_id': 3079037, 'author_profile': 'https://Stackoverflow.com/users/3079037', 'pm_score': 1, 'selected': False, 'text': '<p>After I read this thread, I conducted a little experiment, which yielded two distinct, and interesting, findings.</p>\n\n<p>Consider the following.</p>\n\n<pre><code>strInstallString    "1" string\n</code></pre>\n\n<p>The above is copied from the locals window of the Visual Studio debugger. The same value is used in all three of the following examples.</p>\n\n<p><strong>if ( strInstallString == "" ) === if ( strInstallString == string.Empty )</strong></p>\n\n<p>Following is the code displayed in the disassembly window of the Visual Studio 2013 debugger for these two fundamentally identical cases.</p>\n\n<pre><code>if ( strInstallString == "" )\n003126FB  mov         edx,dword ptr ds:[31B2184h]\n00312701  mov         ecx,dword ptr [ebp-50h]\n00312704  call        59DEC0B0            ; On return, EAX = 0x00000000.\n00312709  mov         dword ptr [ebp-9Ch],eax\n0031270F  cmp         dword ptr [ebp-9Ch],0\n00312716  sete        al\n00312719  movzx       eax,al\n0031271C  mov         dword ptr [ebp-64h],eax\n0031271F  cmp         dword ptr [ebp-64h],0\n00312723  jne         00312750\n\nif ( strInstallString == string.Empty )\n00452443  mov         edx,dword ptr ds:[3282184h]\n00452449  mov         ecx,dword ptr [ebp-50h]\n0045244C  call        59DEC0B0        ; On return, EAX\xa0=\xa00x00000000.\n00452451  mov         dword ptr [ebp-9Ch],eax\n00452457  cmp         dword ptr [ebp-9Ch],0\n0045245E  sete        al\n00452461  movzx       eax,al\n00452464  mov         dword ptr [ebp-64h],eax\n00452467  cmp         dword ptr [ebp-64h],0\n0045246B  jne         00452498\n</code></pre>\n\n<p><strong>if ( strInstallString == string.Empty ) Isn\'t Significantly Different</strong></p>\n\n<pre><code>if ( strInstallString.Length == 0 )\n003E284B  mov         ecx,dword ptr [ebp-50h]\n003E284E  cmp         dword ptr [ecx],ecx\n003E2850  call        5ACBC87E        ; On return, EAX\xa0=\xa00x00000001.\n003E2855  mov         dword ptr [ebp-9Ch],eax\n003E285B  cmp         dword ptr [ebp-9Ch],0\n003E2862  setne       al\n003E2865  movzx       eax,al\n003E2868  mov         dword ptr [ebp-64h],eax\n003E286B  cmp         dword ptr [ebp-64h],0\n003E286F  jne         003E289C\n</code></pre>\n\n<p>From the above machine code listings, generated by the NGEN module of the .NET Framework, version 4.5, I draw the following conclusions.</p>\n\n<ol>\n<li><p>Testing for equality against the empty string literal and the static string.Empty property on the System.string class are, for all practical purposes, identical. The only difference between the two code snippets is the source of the first move instruction, and both are offsets relative to ds, implying that both refer to baked-in constants.</p></li>\n<li><p>Testing for equality against the empty string, as either a literal or the string.Empty property, sets up a two-argument function call, which indicates <em>inequality</em> by returning zero. I base this conclusion on other tests that I performed a couple of months ago, in which I followed some of my own code across the managed/unmanaged divide and back. In all cases, any call that required two or more arguments put the first argument in register ECX, and and the second in register EDX. I don\'t recall how subsequent arguments were passed. Nevertheless, the call setup looked more like __fastcall than __stdcall. Likewise, the expected return values always showed up in register EAX, which is almost universal.</p></li>\n<li><p>Testing the length of the string sets up a one-argument function call, which returns 1 (in register EAX), which happens to be the length of the string being tested.</p></li>\n<li><p>Given that the immediately visible machine code is almost identical, the only reason that I can imagine that would account for the better performance of the string equality over the sting length reported by <strong>Shinny</strong> is that the two-argument function that performs the comparison is significantly better optimized than the one-argument function that reads the length off the string instance.</p></li>\n</ol>\n\n<p><strong>Conclusion</strong></p>\n\n<p>As a matter of principle, I avoid comparing against the empty string as a literal, because the empty string literal can appear ambiguous in source code. To that end, my .NET helper classes have long defined the empty string as a constant. Though I use <strong>string.Empty</strong> for direct, inline comparisons, the constant earns its keep for defining other constants whose value is the empty string, because a constant cannot be assigned <strong>string.Empty</strong> as its value.</p>\n\n<p>This exercise settles, once and for all, any concern I might have about the cost, if any, of comparing against either <strong>string.Empty</strong> or the constant defined by my helper classes.</p>\n\n<p>However, it also raises a puzzling question to replace it; why is comparing against <strong>string.Empty</strong> more efficient than testing the length of the string? Or is the test used by Shinny invalidated because by the way the loop is implemented? (I find that hard to believe, but, then again, I\'ve been fooled before, as I\'m sure you have, too!)</p>\n\n<p>I have long assumed that <strong>system.string</strong> objects were counted strings, fundamentally similar to the long established Basic String (BSTR) that we have long known from COM.</p>\n'}]
['https://Stackoverflow.com/questions/10230', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/288/']