472,958 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1181
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

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.