End of line check problem

This seems to be a very strange problem that I cannot figure out for the life of me. I have a path (string) that looks like this:

D: \ development \ php \ bchat \ chat \ index.php

I need to check if a file in a PHP file is. I believe the most logical way is to substring starting with. to the end of the line and see if it is ==. php

So, I tried:

bool isphp = (path.Substring(path.LastIndexOf('.')) == ".php") ? true : false;

      

This always returns false. I thought maybe there was a trailing space at the end that was wrapping me around, so I put a TrimEnd () in front of it. But that didn't change anything. So I tried this:

bool isphp = (path.EndsWith(".php") == true) ? true : false;

      

This also always returns false.

EDIT I also tried this:

bool isphp = (Path.GetExtension(path) == ".php");

      

But this also returns false.

+1


a source to share


10 replies


The following code works fine on my machine:

    public static void Main()
    {
        string path = @"D:\development\php\bchat\chat\index.php";
        bool isPhp = path.EndsWith(".php");
        Console.WriteLine(isPhp);
    }

      

So, I would guess that there is something else in your line that causes it to not work. It might be a random thing, in which case add StringComparison.InvariantCultureIgnoreCase to your EndsWith like this.

    public static void Main()
    {
        string path = @"D:\development\php\bchat\chat\index.pHp";
        bool isPhp = path.EndsWith(".php", StringComparison.OrdinalIgnoreCase);
        Console.WriteLine(isPhp);
    }

      

If that doesn't work, place the breakpoint in the comparison line and then enter it in the Immediate window:



path[path.Length-1]

      

You should get this as a result:

112 'p'

      

If you don't, you can tell that your path doesn't end with the standard p character.

+3


a source


Use the Path class. It has a GetExtension () method:

var path =  @"D:\development\php\bchat\chat\index.php";
if( Path.GetExtension( path.ToUpperInvariant() ) == ".PHP" )
{}

      



EDIT: Added check for upper / lower case

+8


a source


If it is an existing file, you can get a FileInfo object for it and check the extension like so:

FileInfo fi = new FileInfo(@"D:\development\php\bchat\chat\index.php");
if (fi.Exists && fi.Extension == ".php")
{
    //Do something
}

      


Or, I suppose you can be sheep, follow the crowd and use the much better Path.GetExtension method that everyone else has suggested. But first ask yourself this question: do you want to do it in the cleanest, fastest and best way possible, or do you want to assert your individuality and join me on the path of greatest resistance?

+2


a source


Just a sample for the solution posted:

bool isphp = Path.GetExtension(path)
                 .Equals(".php", StringComparison.InvariantCultureIgnoreCase);

      

+2


a source


Use Path.GetExtension and compare with the file type you are looking for.

+1


a source


How about the Path.GetExtension method?

+1


a source


UPDATE:

Check out what really returns path.Substring(path.LastIndexOf('.')

- it might point you in the right direction.

+1


a source


Make sure there is no difference in upper / lower case. You are only testing lowercase "php".

string ext = Path.GetExtension(path);
bool isPhp = (ext.Equals(".php", StringComparison.InvariantCultureIgnoreCase));

      

+1


a source


private static bool IsPhp(string fileName)
{
    return string.Compare(Path.GetExtension(fileName), ".php",
        StringComparison.OrdinalIgnoreCase) == 0;
}

      

+1


a source


I suspect there is something "weird" about your line. I suggest you lay out your line in detail, for example:

foreach (char c in path)
{
    Console.WriteLine("'{0}' U+{1:x4}", c, (int) c);
}

      

This way you will see unexpected symbols, for example. unicode char 0s between "real" characters.

+1


a source







All Articles