Extract file extension: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(Rewritten.)
Line 1: Line 1:
{{Draft task}}
{{draft task}}


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 (.).
Write a program that takes one string argument representing the path to a file and returns the files extension, or the null string if the file path has no extension. An extension appears after the last period in the file name and consists of one or more letters or numbers.
Example:


Show here the action of your routine on the following examples:
picture.jpg returns .jpg


http://mywebsite.com/picture/image.png returns .png
# picture.jpg returns .jpg
# <nowiki>http://mywebsite.com/picture/image.png</nowiki> returns .png

myuniquefile.longextension returns .longextension
# myuniquefile.longextension returns .longextension
# IAmAFileWithoutExtension returns an empty string ""

# /path/to.my/file returns an empty string as the period is in the directory name rather than the file
IAmAFileWithoutExtension return an empty string ""
# file.odd_one returns an empty string as an extension (by this definition), cannot contain an underscore.





Revision as of 10:14, 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 representing the path to a file and returns the files extension, or the null string if the file path has no extension. An extension appears after the last period in the file name and consists of one or more letters or numbers.

Show here the action of your routine on the following examples:

  1. picture.jpg returns .jpg
  2. http://mywebsite.com/picture/image.png returns .png
  3. myuniquefile.longextension returns .longextension
  4. IAmAFileWithoutExtension returns an empty string ""
  5. /path/to.my/file returns an empty string as the period is in the directory name rather than the file
  6. file.odd_one returns an empty string as an extension (by this definition), cannot contain an underscore.


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>