Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 14th, 2006, 01:19 PM
Registered User
 
Join Date: Mar 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default C# String.IndexOf matching at the beginning.

It thorws me off base when String.IndexOf finds a matching string at the beginning of the string and returns 0 (zero) as the position.

Ex:

String aString = "L1 OR L3 or L2"

What would be the value from, aString.IndexOf("L1")? It is 0.

As per MSDN documentation,

The index position of value if that string is found, or -1 if it is not. If value is Empty, the return value is 0. - This does not seem right?
 
Old March 14th, 2006, 07:58 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It makes sense, if the value is empty ( example: String.IndexOf() ) then the return value is 0.

In your case aString.IndexOf("L1") should return 0 since C# uses 0 based indices.

Use an if statement if you want to catch the case where the value is 0.

Example:
if ( value.Length() == 0 )
    //Do something
else
    location = aString.IndexOf( value );

- A.Kahtava
 
Old March 17th, 2006, 05:49 PM
Registered User
 
Join Date: Mar 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That's what I endup doing though!

Thank you.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Find Matching pattern in Given String sanjivbshinde ASP.NET 2.0 Professional 3 October 10th, 2008 06:32 PM
Regular expressions and string matching DennyLoi Java Basics 0 November 15th, 2007 01:37 PM
Regular Expression matching sub string mayasht Pro Java 3 August 8th, 2007 05:32 AM
Advanced string matching in MySQL hairsoup SQL Language 2 October 8th, 2005 07:28 PM
indexOf crmpicco Javascript How-To 1 September 30th, 2005 09:02 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.