473,379 Members | 1,367 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

indexOf question

vs2005 c#
string var = string.Empty;
why "76090,76091,76092,G0202,G0204,G0206".IndexOf( var)
returns 0 ?

should be -1 for not found...empty is not part of my string being searched
any ideas
thanks
Jun 27 '08 #1
3 1186
raulavi wrote:
vs2005 c#
string var = string.Empty;
why "76090,76091,76092,G0202,G0204,G0206".IndexOf( var)
returns 0 ?

should be -1 for not found...empty is not part of my string being searched
any ideas
thanks
This was literally *just* discussed a couple threads back:
http://groups.google.com/group/micro...3be990c83f93f#

Chris.
Jun 27 '08 #2
Hi,

In case you want to test for an empty string use

if(string.IsNullOrEmpty("Hello World"))
{
// string is either null or ""
}

--
Happy Coding!
Morten Wennevik [C# MVP]
"raulavi" wrote:
vs2005 c#
string var = string.Empty;
why "76090,76091,76092,G0202,G0204,G0206".IndexOf( var)
returns 0 ?

should be -1 for not found...empty is not part of my string being searched
any ideas
thanks
Jun 27 '08 #3


"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:E6**********************************@microsof t.com...
Hi,

In case you want to test for an empty string use

if(string.IsNullOrEmpty("Hello World"))
{
// string is either null or ""
}

--
Happy Coding!
Morten Wennevik [C# MVP]
I would be hard pressed to use IsNullOrEmpty to check for an empty string.
This is due to the fact that Null means the absence of a value while an
empty string is a string whose value contains no characters (hence empty
string). IsNullOrEmpty is the same as:

("Hello World" == null || "Hello World" == string.Empty)

while checking for an empty string is:

("Hello World" == string.Empty)

Therefore, they are not functionally equivalent. Although, I would usually
use IsNullOrEmpty in place of just an empty string check in *most* cases.
Some cases, you may not want to check for null, maybe you need to do
something different for null strings (raise an exception?) compared to empty
strings (allowed so no exception?).

HTH,
Mythran
Jun 27 '08 #4

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

Similar topics

5
by: BILL | last post by:
Hi Everyone, I've been looking through these .NET groups and can't find the exact answer I want, so I'm asking. Can someone let me know the best way (you feel) to search a C# string for an...
12
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
2
by: kd | last post by:
Hi All, Consider the following code. Dim str As String Dim i As Integer str = " Hello Hi" If str.IndexOf("Abc Def") Or str.IndexOf("Ghi jkl mno") Or _ str.IndexOf("pqr stu vwxe") Or...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
11
by: Al | last post by:
This statement returns a -1, indicating "not found": Find1 = Array.IndexOf(FilesArray, "sa001") But IndexOf on a specific item in that array returns a value of 26: Find1 =...
10
by: JohnR | last post by:
I have an arraylist of string values. I would like to search the arraylist to find the index of a particular string and I would like the search to be case insensitive. dim al as new arraylist...
4
by: sh | last post by:
The IndexOf string method returns the position of the first occurrence of a character in a string. Is there a method to find the x occurrence of a character, for example, the 28th occurrence of a...
7
by: Dale | last post by:
I have a design question. I am creating a custom collection of products. The unique key for the products is productId which is an integer. By default, IndexOf(object obj), when obj is an int,...
5
by: Chizl | last post by:
If I know what the first 12 bytes of a string is, which would be faster to validate those first 12 bytes? if(szText.Substring(0,12) == "datagoeshere") or if(szText.IndexOf("datagoeshere")==0)
9
by: maminx | last post by:
hello i have this script in javascript type delimiter = "."; formatCoa = "xxxx"+delimiter+"xxx"+delimiter+"xxx"; alert(formatCoa.indexOf(delimiter)); but the result is i only get the first...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.