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.
a source to share
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.
a source to share
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?
a source to share
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.
a source to share