473,385 Members | 1,492 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,385 software developers and data experts.

String LastIndexOf bug ?

Hi,

string s = "aXXa";
Console.WriteLine(s.LastIndexOf("XX",0));
Console.WriteLine(s.LastIndexOf("XX"));
Console.WriteLine(s.IndexOf("XX",0));
Console.WriteLine(s.IndexOf("XX"));

Result:
-1 (LastIndexOf must also return 1, but gives -1) Is it a bug ?
1 - ok
1 - ok
1 - ok

In documentation it says:
Reports the index position of the last occurrence of a specified String
within this instance. The search starts at a specified character position.
Oct 1 '05 #1
2 3522
No it's not a bug: The LastIndexOf() search works backwards from the
position specified. Hence there is no match for XX from 0.

"Ivar" <iv*@lumisoft.ee> wrote in message
news:O%****************@TK2MSFTNGP10.phx.gbl...
Hi,

string s = "aXXa";
Console.WriteLine(s.LastIndexOf("XX",0));
Console.WriteLine(s.LastIndexOf("XX"));
Console.WriteLine(s.IndexOf("XX",0));
Console.WriteLine(s.IndexOf("XX"));

Result:
-1 (LastIndexOf must also return 1, but gives -1) Is it a bug ?
1 - ok
1 - ok
1 - ok

In documentation it says:
Reports the index position of the last occurrence of a specified String
within this instance. The search starts at a specified character position.

Oct 1 '05 #2
The search begins at the startIndex character position of this instance and
precedes backwards towards the beginning until either value is found or the
first character position has been examined.
Yes I see now.

"Siva M" <sh******@online.excite.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... No it's not a bug: The LastIndexOf() search works backwards from the
position specified. Hence there is no match for XX from 0.

"Ivar" <iv*@lumisoft.ee> wrote in message
news:O%****************@TK2MSFTNGP10.phx.gbl...
Hi,

string s = "aXXa";
Console.WriteLine(s.LastIndexOf("XX",0));
Console.WriteLine(s.LastIndexOf("XX"));
Console.WriteLine(s.IndexOf("XX",0));
Console.WriteLine(s.IndexOf("XX"));

Result:
-1 (LastIndexOf must also return 1, but gives -1) Is it a bug ?
1 - ok
1 - ok
1 - ok

In documentation it says:
Reports the index position of the last occurrence of a specified String
within this instance. The search starts at a specified character position.

Oct 1 '05 #3

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

Similar topics

1
by: Matt | last post by:
In test() method: var path="C:\test\hello.txt"; //returns -1 for path.lastIndexOf("\\"). why?? var pos=path.lastIndexOf("\\"); //return -1 But in showFile() method: We are able to get the...
11
by: ALI-R | last post by:
why c# function for string manupolation is not as strong as other languages ,for extracting a substring from a middle of another string ,I have a lot of problems. Any suggestion or I am in the...
3
by: Brian Shannon | last post by:
Is there a VB.NET function to find the right most characters of a string similiar to Microsoft.VisualBasic.Right? I am currently using the below code to find the right most characters and am...
4
by: Keith Kowalski | last post by:
I am passing in a string of varied length Example1: mil2345_23.lst Example2: mil23456_1.lst Example3: mil5567_1234.lst What I need is to parse out all text after the _ (underscore) and before...
9
by: koorb | last post by:
I am trying to read a line from a text file and store it in a DateTime veritable. Below is a cut down version of my problem code (in my actual version I am using a try and catch, and I am receiving...
2
by: Ivar | last post by:
Hi, string s = "aXXa"; Console.WriteLine(s.LastIndexOf("XX",0)); Console.WriteLine(s.LastIndexOf("XX")); Console.WriteLine(s.IndexOf("XX",0)); Console.WriteLine(s.IndexOf("XX")); Result:...
15
by: Daren | last post by:
Hi, I need to be able to split large string variables into an array of lines, each line can be no longer than 70 chars. The string variables are text, so I would additionally like the lines...
9
by: senfo | last post by:
I realize it's Friday and I'm probably already on vacation for the remainder of the day; but, I have a really, really stupid question. Is there a bug in the .NET 2.0 Framework in regards to the...
3
by: Mathias Weyel | last post by:
Hi Group! I encounter a strange behaviour of String.LastIndexOf when specifying start and count values. The following works: string tempString = orgString.SubString(0, mycount); int index...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.