Remove characters from a string between two specific java characters

Asked

Viewed 276 times

1

I have a String which contains the file path, and I need to remove the characters that come after the last "/" to the end of the string. Can anyone help me with this? ...

Example:

String comando = "C:/Users/Vinicius/Documents/NetBeansProjects/ProjetoORI/retiramarca.exe"

I want to erase the "retiramarca.exe"

 caminho = pesquisaInfo.getText();
    chave = recebeConfirme.getText();

    caminhoInvertido = caminho.replace("\\", "/");

if (actionCommand.equals("Encrypt File")) {

    String comando = "C:/Users/Vinicius/Documents/NetBeansProjects/ProjetoORI/inseremarca.exe " + chave + " " + caminhoInvertido;
    try {
        System.out.println(caminhoInvertido.getParentFile());
        Runtime.getRuntime().exec("cmd.exe /C start " + comando);

    } catch (IOException ex) {
    }
    System.out.println(actionCommand);
    JOptionPane.showMessageDialog(null, "Arquivo criptografado");
  • 3

    You don’t need regex for that. Add the code there that you are using, there are no regex solutions to recover it.

  • 2

    Not only does it not need regex, but it wouldn’t be the most appropriate, because what you want is to go to the previous folder. You should do as @Articuno indicated

  • 2

    All life you catch an exception and don’t treat it properly, a fairy dies

2 answers

4


If you are using the class File, it is possible to recover this information without regex, through the method getParent(). It returns the folder immediately above the current file path.

String caminho = "C:/Users/Vinicius/Documents/NetBeansProjects/ProjetoORI/inseremarca.exe";

System.out.println(new File(caminho).getParent());

Exit:

C: Users Vinicius Documents Netbeansprojects Projetoori

See works on IDEONE

The method getParent() returns a string with the path at a level above the file location.

  • I’m not using the File class. In fact, the string is getting its way through a gettext from a Jtextfield, which in turn received the path through the Jfilechooser. I need to pass this variable by argument with Runtime.getRuntime, so that my C script gets this path and goes to the destination to fetch the file and encrypt it. I’ll show you the code snippet...

  • @M.Vinicius and why not convert to file, rescue the path and cast to string? This is less expensive than doing regex,

  • I hadn’t thought of it that way, it’s a good one. I’m starting programming in java now.. rs

  • @M.Vinicius See the edition.

  • I tested it here, that’s just what I need. Thank you! ... There’s only one thing, I can put that number in a string, because I need to deal with the bar issue, turn it upside down.

  • @M.Vinicius sorry, but I made a significant change in the answer, the getparent() method does the same thing, only it does not need to convert into string, the return is already of this type.

  • all right, I got the treatment I needed. Thanks for your help!

  • @M.Vinicius do not know if you have already done this, but an optimization tip is to take the variable caminho with the "filename.extensao" of your text field, make the answer code and swap the bars afterwards. So you don’t have to make the switch twice.

  • I did just that... it’s working perfectly!

Show 4 more comments

2

Use the package classes java.nio.* to handle and manipulate file and directory paths

Path path = Paths.get("C:/Users/Vinicius/Documents/NetBeansProjects/ProjetoORI/inseremarca.exe")
                 .getParent();

// C:\Users\Vinicius\Documents\NetBeansProjects\ProjetoORI
System.out.println(path);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.