Fraser Munro
Fraser Munro

Reputation: 21

Substring file path

Hi I'm trying to substring a file path before it goes into a dictionary. I've tried to declare the start point but am given an error:

StartIndex cannot be larger than length of string. Parameter name: startIndex

This is my code

private  Dictionary<string,int> CreateDictionary(log logInstance)
        {
            Dictionary<string, int> dictionary = new Dictionary<string, int>();
            for (int entryIdex = 0; entryIdex < logInstance.logentry.Count(); entryIdex++)
            {
                logLogentry entry = logInstance.logentry[entryIdex];
                for (int pathIdex = 0; pathIdex < entry.paths.Count(); pathIdex++)
                {
                    logLogentryPath path = entry.paths[pathIdex];
                    string filePath = path.Value;
                    filePath.Substring(63);
                    string cutPath = filePath;
                    if (dictionary.ContainsKey(cutPath))
                    {
                        dictionary[cutPath]++;
                    }
                    else
                    {
                        dictionary.Add(cutPath, 1);
                    }
                }
            }
            return dictionary;
        }

Any help would be great.

I've also tried doing

filePath.Substring(0, 63);

and

filePath.Substring(63, length);

Upvotes: 1

Views: 1009

Answers (4)

Gusdor
Gusdor

Reputation: 14322

Reading your comments, it appears you simply want the file name from the file path. There is a built in utility for achieving this on any path.

From MSDN:

Path.GetFileName

Returns the file name and extension of the specified path string.

https://msdn.microsoft.com/en-us/library/system.io.path.getfilename%28v=vs.110%29.aspx

Here is a code sample to get you started.

string path = @"/GEM4/trunk/src/Tools/TaxMarkerUpdateTool/Tax Marker Ripper v1/Help_Document.docx";

string filename = System.IO.Path.GetFilename(path);

Console.WriteLine(filename);

Output

Help_Document.docx

And here is your code with the modification;

Private  Dictionary<string,int> CreateDictionary(log logInstance)
    {
        Dictionary<string, int> dictionary = new Dictionary<string, int>();
        for (int entryIdex = 0; entryIdex < logInstance.logentry.Count(); entryIdex++)
        {
            logLogentry entry = logInstance.logentry[entryIdex];
            for (int pathIdex = 0; pathIdex < entry.paths.Count(); pathIdex++)
            {
                logLogentryPath path = entry.paths[pathIdex];
                string filePath = path.Value;

                // extract the file name from the path
                string cutPath = System.IO.Path.GetFilename(filePath);

                if (dictionary.ContainsKey(cutPath))
                {
                    dictionary[cutPath]++;
                }
                else
                {
                    dictionary.Add(cutPath, 1);
                }
            }
        }
        return dictionary;
    }

Upvotes: 0

Fraser Munro
Fraser Munro

Reputation: 21

Thanks Everyone for the help

My code now works and looks like this:

Private  Dictionary<string,int> CreateDictionary(log logInstance)
    {
        Dictionary<string, int> dictionary = new Dictionary<string, int>();
        for (int entryIdex = 0; entryIdex < logInstance.logentry.Count(); entryIdex++)
        {
            logLogentry entry = logInstance.logentry[entryIdex];
            for (int pathIdex = 0; pathIdex < entry.paths.Count(); pathIdex++)
            {
                logLogentryPath path = entry.paths[pathIdex];
                string filePath = path.Value;

                if (filePath.Length > 63)
                {
                    string cutPath = filePath.Substring(63);
                    if (dictionary.ContainsKey(cutPath))
                    {
                        dictionary[cutPath]++;
                    }
                    else
                    {
                        dictionary.Add(cutPath, 1);
                    }
                }
            }
        }
        return dictionary;
    }

Upvotes: 0

Volodymyr Sichka
Volodymyr Sichka

Reputation: 561

First:

// It's do nothing
filePath.Substring(63);

// Change to this
filePath = filePath.Substring(63);

Second:

63 is the character I was to substring from the file path for every entry looks like this: /GEM4/trunk/src/Tools/TaxMarkerUpdateTool/Tax Marker Ripper v1 with the final bit the real information i want to be shown which is: /DataModifier.cs

It's a bad idea to use 63. Better find last "/" and save it position to some variable.

Upvotes: 0

Tom Biddulph
Tom Biddulph

Reputation: 534

Strings in C# are immutable (once a string is created it cannot be modified), this means that when you set string cutpath = filepath you are setting the value of cutpath to path.Value as you haven't assigned the value of filepath.SubString(63) to anything. To fix this change

string filePath = path.Value;
filePath.Substring(63); // here is the problem
string cutPath = filePath;

To

string filePath = path.Value;
string cutPath = filePath.Substring(63);

Upvotes: 2

Related Questions