473,666 Members | 2,075 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string.lenght vs string.empty

AVL
Hi,
I read that to check for empty strings , comparing strings using
String.Length == 0 is faster than String.Empty or "" . Is it true...?
If so, how?
Nov 19 '05 #1
4 1426
AVL wrote:
Hi,
I read that to check for empty strings , comparing strings using
String.Length == 0 is faster than String.Empty or "" . Is it true...?
If so, how?


I think it is faster because:
* Length is stored, doesn't need to be calculated
* integer comparison (Length == 0) is easier that string comparison,
which (I think) needs to be done on a character-by-character basis.

Hans Kesting
Nov 19 '05 #2
On Wed, 27 Apr 2005 22:46:02 -0700 in
microsoft.publi c.dotnet.framew ork.aspnet, AVL
<AV*@discussion s.microsoft.com > wrote:
Hi,
I read that to check for empty strings , comparing strings using
String.Lengt h == 0 is faster than String.Empty or "" . Is it true...?
If so, how?


I think str.Length is probably faster, however if your string is null,
using Length will throw a NRE. I prefer to use
string.IsNullOr Empty(someStrin g). It safely works on null strings as
well as empty strings.

Roger

Nov 19 '05 #3
Under normal circumstances it's unlikely to give a perceptible difference...

You could easily measure and see what the IL is...

Patrice

--

"AVL" <AV*@discussion s.microsoft.com > a écrit dans le message de
news:1F******** *************** ***********@mic rosoft.com...
Hi,
I read that to check for empty strings , comparing strings using
String.Length == 0 is faster than String.Empty or "" . Is it true...?
If so, how?

Nov 19 '05 #4
It is true that checking the String.Length is faster.
Numeric functions are almost always more efficient than string functions.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

"AVL" <AV*@discussion s.microsoft.com > wrote in message
news:1F******** *************** ***********@mic rosoft.com...
Hi,
I read that to check for empty strings , comparing strings using
String.Length == 0 is faster than String.Empty or "" . Is it true...?
If so, how?

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3825
by: Mathieu Malaterre | last post by:
Hello, I guess this might be a very dummy question, but I couldn't find an answer in the group's archive. I am reading DICOM files, and to store the string read I use std::string. Unfortunately there is case where the string can be '\0\0' Is this a defined bahavior when you do this kind of operation : const char *s = "\0\0";
9
27056
by: Niels Dekker - no reply address | last post by:
Are all the following initializations semantically equivalent? wchar_t a = {L'\0'}; wchar_t b = {'\0'}; wchar_t c = {0}; wchar_t d = {}; If so, why don't we all use an empty initializer list, {}, when zero-initializing a C-style string? I was wondering, because the book C++ Coding Standards (Sutter & Alexandrescu) says at item 19, "Always
4
29112
by: Robert | last post by:
Hi, How can i resize an array of strings to add more? This is an example i just downloaded from a website: char *cpArray; int i; for (i = 0; i < 10; i++) cpArray = (char *)malloc(20 * sizeof(char)); strcpy(cpArray, "A hat is on the mat");
12
2481
by: Vicky | last post by:
What is the better way of checking blank string is it strvar.lenght > 0 or strvar == "" or something else Thnaks
1
1876
by: smeagol | last post by:
I have a varchar 255 field in the Sql. When i get the data with (string)myReader; it's trimed ie( SQL : "qwerty " (Lenght 255) c# : (string)myReader.Lenght; (=5) It's possible to get (string)myReader.Lenght; (=255)
9
2364
by: Søren M. Olesen | last post by:
Hi Can someone tell me how to implement something similar to String.Empty ?? Given a class called myValue Public class myValue private id as integer private value as string end class
8
7815
by: cdolphin88 | last post by:
Hi, I'm reading from a .dat file and I want to know the length of the string . I' using the stringRef.length () but the compiler said there was an error `stringRef' undeclared (first use this function)
4
3167
by: javaalien | last post by:
Could someone please help me how to count lenght string in a file? I have input file like this: 1039387845 35368535615253 695 .... Kindly pls assist me how to count the lenght. Thank you in adv. String lineA = ""; BufferedReader inputA = new BufferedReader (new FileReader("A.txt"));
38
3902
by: Andrea | last post by:
Hi, Anyone could me suggest how to create a function that generates a random string? The function should be: int _RandomString(char** str,int len); so, it takes an empty string str and it puts in in a random string of lenght len, and it returns 1 for success 0 otherwise. Thanks in advance,
0
8871
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8783
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8552
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2773
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.