Home > Mobile >  Separating Filename from Filepath String
Separating Filename from Filepath String

Time:02-25

I'm separating a filepath string into two separate strings:

  1. filepath with just directories (filename removed)
  2. filename

I'd like to work within a CPS context, which prevents some closure usage. I've got the following working code, but I'm unsure if it is good practice. The filepath references an "ID Version" file named 'idver.h.' 'projectPaths' is a map containing the filepath.

    //pull off the filename from end of filepath
    String[] idVerPathArray = projectPaths.idVerPath.split('/')
    String filename = idVerPathArray.last()
    //get subarray without last element
    String[] idVerJustPathArray = Arrays.copyOfRange(idVerPathArray, 0, idVerPathArray.length - 1)
    String idVerJustPath = idVerJustPathArray.join('/')

CodePudding user response:

def file = new File(projectPaths.idVerPath)
String filename = file.name
String idVerJustPath = file.parent
  • Related