Link to home
Start Free TrialLog in
Avatar of ansi_c
ansi_c

asked on

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.
Avatar of Zyloch
Zyloch
Flag of United States of America image

Hi

What language are you using?

Regards,
Zyloch
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
Avatar of da99rmd
da99rmd

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
Avatar of ansi_c

ASKER

i am doing unix shell scripting.
SOLUTION
Avatar of da99rmd
da99rmd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
de2Zotjes,
smothe solution there.
/Rob
smothe ?

I am not native english speaking, what am I missing?
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