Extract file extension: Difference between revisions

From Rosetta Code
Content added Content deleted
(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...")
 
No edit summary
Line 1: Line 1:
<includeonly>{{#set:is task=false}}{{#set:is draft=true}}</includeonly>{{infobox_begin}}'' '''{{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:{{PAGENAME}}|talk page]].<includeonly>[[Category:Draft Programming Tasks]] [[Category:{{{1|Draft Programming Tasks}}}]]</includeonly>{{infobox_end}}<noinclude>
{{template}}</noinclude>
{{Draft task}}
{{Draft task}}



Revision as of 09:38, 4 May 2015

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.

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:

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>