Extract file extension

Revision as of 09:37, 4 May 2015 by rosettacode>Sceiler (Created page with "<includeonly>{{#set:is task=false}}{{#set:is draft=true}}</includeonly>{{infobox_begin}}'' '''{{Extract file extension}}''' '' is a '''draft''' programming task. It is not yet...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Write a program that takes one string argument. The string argument has a extension in the end but it is unknown what extension it is. It can be whatever but going from tail to head the extension is marked by a dot (.). Example:

Template:Extract file extension is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

This is a template. There are many others. See Category:RCTemplates for a complete list of templates.
Extract file extension is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

picture.jpg returns .jpg

http://mywebsite.com/picture/image.png returns .png

myuniquefile.longextension returns .longextension

IAmAFileWithoutExtension return an empty string ""


C#

<lang C#> public static string ExtractExtension(string str) {

           string s = str;
           string temp = "";
           string result = "";
           bool isDotFound = false;
           for (int i = s.Length -1; i >= 0; i--)
           {
               if(s[i].Equals('.'))
               {
                   temp += s[i];
                   isDotFound = true;
                   break;
               }
               else
               {
                   temp += s[i];
               }
           }
           if(!isDotFound)
           {
               result = "";
           }
           else
           {
               for (int j = temp.Length - 1; j >= 0; j--)
               {
                   result += temp[j];
               }
           }
           return result;

} </lang>