0

I wanted to know if there is anything like "BeforeLastIndexOf". I'm new to c# and don't really know how "LastIndexOf" and "IndexOf" works. What I'm trying to achive is that for example the user types in a directory and it deletes from the string the last folder of this directory but usually directory's look like this "C:\something\something\" and it has a "\" at the end, so a code like this doesn't work:

string input = Console.ReadLine();
int index = input.LastIndexOf("/");
if (index > 0)
     input = input.Substring(0, index + 1);

Cause it deletes only everything after the last "\" and it's anyway at the end so it doesn't delete anything

SSD
  • 1,373
  • 2
  • 13
  • 20
magiccheese
  • 111
  • 2
  • 9
  • Do you want [`Directory.Delete(input, true);`](https://msdn.microsoft.com/en-us/library/fxeahc5f(v=vs.110).aspx)? This will delete the directory specified (which is the "last" folder in the path), regardless of a trailing slash. – Ron Beyer Feb 25 '18 at 19:28
  • i mean deleting it from the string – magiccheese Feb 25 '18 at 19:30

3 Answers3

2

It looks like you want to:

  1. Remove any trailing backslashes, using string.TrimEnd().
  2. Call Path.GetDirectoryName() to strip off the last directory.

For example:

string path1 = @"C:\something\something\";
Console.WriteLine(Path.GetDirectoryName(path1.TrimEnd('\\'))); // Prints "C:\something

string path2 = @"C:\something\something";
Console.WriteLine(Path.GetDirectoryName(path2.TrimEnd('\\'))); // Prints "C:\something

If you want to handle / characters too (since that's actually a valid path separator in Windows), you can just specify both in the TrimEnd():

path1.TrimEnd('/', '\\')

Thus:

string path3 = @"C:/something/something//\\/";
Console.WriteLine(Path.GetDirectoryName(path3.TrimEnd('/', '\\'))); // Prints "C:\something
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • What you are doing is a kludge. You're pretending like the last segment in the path name is a file name, but it's a directory name. I don't think that hacking .NET is the good way to write software. The proper way to do this is to use `Directory` or `DirectoryInfo` to parse the entire path. – Zoran Horvat Feb 25 '18 at 19:51
  • 1
    @ZoranHorvat It's not a kludge. [The documentation for `Path.GetDirectoryName()`](https://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname(v=vs.110).aspx) specifically describes this behaviour. – Matthew Watson Feb 25 '18 at 20:35
0

I think that you're missing the point with your question. If you want to manipulate directory paths, then you should use types from the System.IO namespace and let them do that right. For example, finding the immediate parent directory given a full path of a directory can be done like this:

string parentPath = new DirectoryInfo(fullPath).Parent.FullName;

If you still want to do that on your own, then there's an overload of LastIndexOf which accepts the range within which to search (https://msdn.microsoft.com/en-us/library/d0z3tk9t(v=vs.110).aspx). However, you have to make sure not to access nonexistent indices within the string.

string final = input;
int secondLast = input.LastIndexOf("/");
if (secondLast >= 0)
{
    secondLast = input.LastIndexOf("/", secondLast - 1, secondLast);
}

if (secondLast >= 0)
{
    final = input.Substring(0, secondLast + 1); // Includes final '/'
}

When this code executes, the secondLast variable will either be set to -1, or to the index of the second last occurrence of /. Note that the variable will also be set to -1 if there is one appearance of /, but not the second one to precede it.

Zoran Horvat
  • 10,924
  • 3
  • 31
  • 43
0

Just use TrimEnd before doing anything else

 input = input.TrimEnd(Path.DirectorySeparatorChar);

This will remove last directory separator ‘/‘ character. After this you can use your further logic to work with path.

SSD
  • 1,373
  • 2
  • 13
  • 20
  • This would needlessly construct an entire new string. – Zoran Horvat Feb 25 '18 at 19:49
  • It will just remove the last unwanted character. And ofCourse new string that you can use further in your logic. If you want same variable then assign back to input. Edited answer @ZoranHorvat – SSD Feb 25 '18 at 20:06
  • @ZoranHorvat - Strings are immutable, any change will create a new string. – Null511 Feb 25 '18 at 21:35
  • @Null511 That's my point - this step would needlessly construct a new string without producing what was required. – Zoran Horvat Feb 25 '18 at 21:36