Extract file extension: Difference between revisions

Content added Content deleted
(→‎{{header|Perl}}: shorter, cleaner code)
(Improved C# implementation)
Line 334: Line 334:


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang [[C sharp|C#]]>
<lang [[C sharp|C#]]>public static string FindExtension(string filename) {
int indexOfDot = filename.Length;
public static string ExtractExtension(string str)
for (int i = filename.Length - 1; i >= 0; i--) {
{
string s = str;
char c = filename[i];
string temp = "";
if (c == '.') {
string result = "";
indexOfDot = i;
bool isDotFound = false;
break;
}

for (int i = s.Length -1; i >= 0; i--)
if (c >= '0' && c <= '9') continue;
{
if (c >= 'A' && c <= 'Z') continue;
if(s[i].Equals('.'))
if (c >= 'a' && c <= 'z') continue;
{
break;
}
temp += s[i];
return filename.Substring(indexOfDot);
isDotFound = true;
}</lang>
break;
}
else
{
temp += s[i];
}
}

if(!isDotFound)
{
result = "";
}
else
{
for (int j = temp.Length - 1; j >= 0; j--)
{
result += temp[j];
}
}

return result;
}
</lang>


=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==