Results 1 to 7 of 7

Thread: string split

  1. #1
    gisler is offline Member
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Default string split

    hi
    for some reason my string split is not working. i am trying to check if a file exists in a directory and then add a number to the file so it does not try to overwrite the existing file. the method looks like this..
    Java Code:
        public String fileExists(String path, String fileName){
            File fileCheck = new File(path.trim()+"\\"+fileName.trim());
            int count=0;
            String newFileName="";
            String[] fileSplit=null;
            while(true){
    
                if (fileCheck.exists()){
                    count++;
                    fileSplit = fileName.split(".");
                      newFileName=fileSplit[0]+Integer.toString(count)+fileSplit[1];
                   }else{
                    break;
                }
                    
            }
            return newFileName;
        }
    thanks

    edit: oh yes when i split the file the fileSplit variable just shows nothing.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Since split uses a regex, what if you escape the period?
    Java Code:
    fileSplit = fileName.split("//.");

  3. #3
    gisler is offline Member
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Default

    oh right. would have not thought of that. thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You're welcome.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    ps. use backslashes; not forward slashes.

    kind regards,

    Jos

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by JosAH View Post
    ps. use backslashes; not forward slashes.
    Garrrrrhhhh, that dyslexia popping through again.

  7. #7
    venugopal is offline Member
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

    Default

    include a break statement inside if loop to break through the while loop... :-)

Similar Threads

  1. I wish to split a string xxx/yyyy/zzz so that ...
    By nmvictor in forum New To Java
    Replies: 10
    Last Post: 10-23-2009, 09:47 AM
  2. 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
  3. split Keyword
    By santhoshrao in forum New To Java
    Replies: 4
    Last Post: 08-13-2008, 10:28 AM
  4. Replies: 3
    Last Post: 12-17-2007, 02:35 AM
  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
  •