user3476500 user3476500 - 1 year ago 103
Java Question

Changing characters in a string (Path) Java

Hello i'm working on program in java, i have a string (a path) in which i want to change the "\" with the "/".

This my program, but i keep having the same path, no changes.

package Test;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Filechooser {

public static void main(String[] args) {

JFileChooser fc=new JFileChooser();
fc.addChoosableFileFilter(new FileNameExtensionFilter("text Documents", "txt"));
int returnVal=fc.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String path= fc.getSelectedFile().getAbsolutePath();
String temp = path.replaceAll("\"", "/");


Answer Source

Try using:

String temp = path.replace("\\", "/");

replaceAll(str, str) takes a regex, not a simple char/string, so you can't use replaceAll like that.

\\ means \ in Java, while \" means ", so be careful with that :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download