Skip to main content

.Net Strings - LastIndexOf in c#

Today, I tried to use string methods IndexOf and LastIndexOf to parse a directory entry string that I received across an SSH channel. I guess I should have read the method description closer, but I expected LastIndexOf to find the last space character in a string. The signature I wanted to use is:

public int LastIndexOf(char, int, int);

Reports the index position of the last occurrence of a specified String within this instance. The search starts at a specified character position and examines a specified number of character positions.

It turns out that LastIndex will decrement it's starting index for "count" characters. Here was my incorrect code:

private void method () {
string dirEntry = "-rwxr-xr-x 1 abcde fghij 44458 Feb 22 1987 prt01.txt";
int m, n;

// I thought this would scan from 32 to 44 and find the last space char
m = dirEntry.LastIndexOf(' ', 32, 12);
// it returned 32

// code that does what I wanted:
m = 32;
for(int i=32; i<44; i++)
{
if (ca[i] != ' ')
{
m = i;
break;
}
}

// find first space after the numeric string
n = dirEntry.IndexOf(' ', m+1, 12);

// if m and n are correct, get the file size
string fsize = dirEntry.Substring(m, n-m);
}

Design Note: I must admit that there are alot of "magic numbers" in here. I normally do not write code with magic numbers embedded in it. I prefer to assign class constants, or use INI, XML or database initialization as appropriate.

Comments

Popular posts from this blog

Castle ActiveRecord with DetachedCriteria

My current development environment is Visual Studio Express C# Edition (read that as free ), Castle ActiveRecord's latest svn trunk(usually within a few days), and NHibernate svn trunk. As of NHibernate version 1.2.0, there is a very cool new class out there ... DetachedCriteria. This class lets you set all of your Castle relational attributes like BelongsTo, HasMany, etc. as lazy fetch, and over-ride this for searches, reports, or anytime you know ahead of time that you will be touching the related classes by calling detachedCriteria.SetFetchMode(..., FetchEnum.Eager). As a good netizen, I have tried to contribute to NHibernate and Castle ActiveRecord even if only in the smallest of ways . Oh yeah, I tried mapping to a SQL VIEW, and it worked GREAT! I received a comment after my last post, indicating that there is a better way, and I am sure of it, but the view guaranteed that I only have one database request for my dataset. NHibernate was wanting to re-fetch my missing as...

How does Rails scaffolding select HTML input tags?

Recently, a reader saw my fix for SQL Server booleans, and asked me a followup question: why does Rails display a yes/no selection instead of a checkbox? The short answer is look in {RUBY_HOME} /lib/ruby/gems/1.8 /gems/actionpack-1.10.2 /lib/action_view/helpers, but your path may vary depending on whether you are using gem, "edge rails", etc. Anyway, look in the file "active_record_helper.rb" for a method called "all_input_tags", and notice that it calls "default_input_block" if you don't supply an input_block. Now notice that "default_input_block" creates a label and calls "input(record, column.name)" which in turn calls "InstanceTag#to_tag" which finally looks at the datatype and maps boolean to a select tag. Perhaps a wiser Rails explorer can provide us with the rationale for this, but I guess we could add a MixIn for InstanceTag that redefines the to_tag() method, or just do a dirty and unmaintainable hack l...

Castle Active Record Code Generator

I have finally released my Code Generator to Google Code as Active-Record-Gen . What does it generate? It generates ActiveRecord classes mainly, but I have used it to generate stored procedures and sys-admin scripts as well. This code generator does not (yet) generate a full Windows application project or a Mono-Rail web site, but the generated code could be used in either. In fact, with a few tweaks, this could be used to generate NHibernate "poco" and .xbm files. If you want to know more, look at the screen shots above, or head over to Google Code and run it. In my haste to make my first EXE release before supper, I forgot to add the Template directory, which should be at the same directory level as the EXE and config files. I just (1.5 hours later) uploaded a new EXE, but 2 people have already downloaded the EXE (not the source though). As for the basic table object, it is built with the following assumptions: Table name is plural, class name is singular. Field ...