Results 1 to 4 of 4
  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default String split question

    Hi,

    I have a filename with the complete filepath which I would like to rename the file, put it in another folder.

    The copying of the file to a new folder work, but I am struggeling with the renaming.

    I am converting the filename to a String:
    Java Code:
    String fileName    = file.toString();
    I get a result looking something like this: "C:\Temp\Arkiv\file01.jpg"

    I am then trying the split method to get the fileextension:
    Java Code:
    String [] splitter = fileName.split(".");
    String file1       = splitter[0];
    String ext         = splitter[1];
    String filename    =  "newfile" + "." + ext;
    But it does not work.
    The splitter is emty (0).

    Any suggestions?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: String split question

    The . is a special character in patterns. you have to mask it with a backslash.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: String split question

    Fantastic! That did the trick:
    Java Code:
    String [] splitter = fileName.split("\\.");
    Thanks!!

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: String split question

    You're welcome :)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Question about split()
    By kraigballa in forum New To Java
    Replies: 6
    Last Post: 03-23-2012, 07:00 AM
  2. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:08 PM
  3. Using String Split
    By JonJacobs in forum New To Java
    Replies: 5
    Last Post: 07-27-2010, 07:43 PM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •