How to find previous character index.

  • Thread starter Thread starter None
  • Start date Start date
N

None

Hi,

I have a string like below.

File:Name of the file*023456
File:Name of the file*023453
File:Name of the file*023436

How to find the Colon(:) index from *index(i know only * position
index). With this index how should i found previous character index(For
example i need Colon index)


Thanks,
Vinoth
 
How to find the Colon(:) index from *index(i know only * position
index). With this index how should i found previous character index(For
example i need Colon index)

yourString.IndexOf(':', 0, theAsteriskIndex);

(or possibly LastIndexOf instead of IndexOf)



Mattias
 
The easiest way to do it is to do what Erick said above. However, if you have
to parse a large piece of text such as following:
====================
File:Name of the file*023451
File:Name of the file*023452
File:Name of the file*023433
File:Name of the file*023454
File:Name of the file*023455
File:Name of the file*023436
File:Name of the file*023457
File:Name of the file*023458
File:Name of the file*023439
====================

.... and you are just trying to pull out the text between the : and the *
followed by the number then I would suggest using a regular expression.

For eaxmple:
==========================
string input = // input is the piece of text above.

MatchCollection matches = Regex.Match(input,
@"(?nmi):(?<FileName>.*)\*\d\d*$");

foreach(Match match in matches)
{
Console.WriteLine(match.Groups["FileName"].Value;
}

==========================
--
Brian Delahunty
Ireland

http://briandela.com/blog

INDA SouthEast - http://southeast.developers.ie/ - The .NET usergroup I
started in the southeast of Ireland.
 
Back
Top