?
Solved

Get Extension

Posted on 2004-08-11
9
Medium Priority
?
171 Views
Last Modified: 2010-04-22
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
Comment
Question by:ansi_c
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 11780831
Hi

What language are you using?

Regards,
Zyloch
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11780834
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
 
LVL 8

Expert Comment

by:da99rmd
ID: 11780998
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
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 

Author Comment

by:ansi_c
ID: 11781904
i am doing unix shell scripting.
0
 
LVL 8

Assisted Solution

by:da99rmd
da99rmd earned 100 total points
ID: 11782224
ansi_c,
then this will apply:
ext=`echo $variable | sed 's/.*\([^\.]\+\)\.\([^\.]\+\)$/\2/'`
/Rob
0
 
LVL 6

Accepted Solution

by:
de2Zotjes earned 100 total points
ID: 11787595
If you are using bash and protability is not an issue you can use:

${variable##*.}
0
 
LVL 8

Expert Comment

by:da99rmd
ID: 11790820
de2Zotjes,
smothe solution there.
/Rob
0
 
LVL 6

Expert Comment

by:de2Zotjes
ID: 11797007
smothe ?

I am not native english speaking, what am I missing?
0
 
LVL 8

Expert Comment

by:da99rmd
ID: 11827942
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 22 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question