[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

Get Extension

Hey.. Suppose I have a variable that stores the below value
    "abc/def/ghi.txt"

I wish to get the extension which in this case would be txt. How to do that? Note that the length of the extension varies.
Full working example preferred.
0
ansi_c
Asked:
ansi_c
  • 4
  • 2
  • 2
  • +1
2 Solutions
 
ZylochCommented:
Hi

What language are you using?

Regards,
Zyloch
0
 
ZylochCommented:
Just to let you know, most often this is done with some function like split() in the language, but we have to be sure to give a working example.

Regards
0
 
da99rmdCommented:
Hi ansi_c,
yes it would be easier if we know what language :)

in php you do like this:

$arr = explode(".",$variable);
$extention = arr[count[arr]];

in bash do this:

ext=`echo $variable | sed 's/.*\([^\.]\+\)\.\([^\.]\+\)$/\2/'`

in c:

bool getextention(char *in, char *out)
{
    char *tmp = in;
    char *punkt;
   
    for(int i=0; i < sizeof(*in);i++;)
        {
            if(tmp++ == '.')
                punkt = tmp;
        }
    if(tmp != in)
        {
            out = tmp;
            return TRUE;
        }
    return FALSE;
}


/Rob
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
ansi_cAuthor Commented:
i am doing unix shell scripting.
0
 
da99rmdCommented:
ansi_c,
then this will apply:
ext=`echo $variable | sed 's/.*\([^\.]\+\)\.\([^\.]\+\)$/\2/'`
/Rob
0
 
de2ZotjesCommented:
If you are using bash and protability is not an issue you can use:

${variable##*.}
0
 
da99rmdCommented:
de2Zotjes,
smothe solution there.
/Rob
0
 
de2ZotjesCommented:
smothe ?

I am not native english speaking, what am I missing?
0
 
da99rmdCommented:
de2Zotjes,
sry I cant spell it should be "smooth" and it means nice, easy in this purpose otherwise it can mean flat, even.
However it was a compliment :)
/Rob
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now