11 Replies Latest reply: Aug 26, 2009 2:19 PM by 798640 RSS

    Java String Manipulation

    807580
      Hi
      I am looking a Java String method to remove the file name extension from the file name. I want to do like below

      filename="myfile.txt"; out put should be --> myfile
      filename="myfile.docx"; out put -----------------> myfile
      filename="myfile1.2 version" out put ----------------->myfile1.2 version
      filename="myfile" output-------------------> myfile
      filename="myfile1.2 version.doc"; output----------------> myfile1.2 version

      any help highly appreciated
      thanks
        • 1. Re: Java String Manipulation
          807580
          Just check what methods [java.lang.String|http://java.sun.com/javase/6/docs/api/java/lang/String.html] provides to substring a string and to get the last index of a certain character. Then just do the math.
          • 2. Re: Java String Manipulation
            807580
            So what have you tried?
            What troubles are you having?
            • 3. Re: Java String Manipulation
              807580
              I am having trouble, if I have the filename like "myfile1.2 version. it cut the rest of the "." part. getting the out put like "myfile1"



              {code}
              String filename="myfile1.2 version";

              if (filename.lastIndexOf('.') != -1){

              filename = filename.substring(0, (filename.lastIndexOf('.'));
              System.out.println("The filename is:"+filename);
              }
              {code}
              I want the out put is : myfile1.2 version
              • 4. Re: Java String Manipulation
                805879
                There is a string method that let you find the last occurance of a single character. fins the last "." and then erase from there.
                • 5. Re: Java String Manipulation
                  807580
                  Polther_Geist wrote:
                  There is a string method that let you find the last occurance of a single character. fins the last "." and then erase from there.
                  But as OP has stated, it doesn't work for files like myfile 1.2 version and I do not see any way around this if you allow that as a valid file name.
                  • 6. Re: Java String Manipulation
                    EJP
                    I am looking a Java String method to remove the file name extension from the file name.
                    The extension is usually defined as the part after the last dot.
                    I want to do like below

                    filename="myfile.txt"; out put should be --> myfile
                    Agreed.
                    filename="myfile.docx"; out put -----------------> myfile
                    Agreed.
                    filename="myfile1.2 version" out put ----------------->myfile1.2 version
                    Disagreed. Why should this case be different?
                    filename="myfile" output-------------------> myfile
                    Agreed.
                    filename="myfile1.2 version.doc"; output----------------> myfile1.2 version
                    Agreed.
                    any help highly appreciated
                    Your expectations are wrong. At least you haven't defined exactlyprecisely what you mean by 'extension'. When you do that, you can code it easily enough. Until you do, you can't.
                    • 7. Re: Java String Manipulation
                      807580
                      Your expectations are wrong. At least you haven't defined exactlyprecisely what you mean by 'extension'. When you do that, you can code it easily enough. Until you do, you can't 
                      Its a valid file name for my case, the file with no format. anyway I found a way to solve this, I will post it soon

                      thank all for the effort
                      • 8. Re: Java String Manipulation
                        YoungWinston
                        vinuemmanuel wrote:
                        Its a valid file name for my case, the file with no format. anyway I found a way to solve this, I will post it soon
                        I hope you're not planning on running it anywhere but Windows.

                        Winston
                        • 9. Re: Java String Manipulation
                          EJP
                          Its a valid file name for my case
                          Quite possibly. You still have to sort out your ambiguous specification of 'extension'. Only you know exactly what you mean.
                          • 10. Re: Java String Manipulation
                            807580
                            Maybe in his opinion an extension is only valid when it matches one of the file extensions registered at the underlying operating system.
                            • 11. Re: Java String Manipulation
                              798640
                              It is relatively safe to assume that a file extension has no spaces in it. It doesn't cover all cases but that's a good start.
                              Note: to complicate things, some softwares like to make compound extensions, like Greasemonkey with its .user.js... Or even the old .tar.gz, although the latter covers actually two states of the file.