你好我正在使用java程序,我有一個字符串(一個路徑),我想用「/」來改變「\」。更改字符串中的字符(路徑)Java
這是我的程序,但我保持着相同的路徑,沒有變化。
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.setApproveButtonText("Open");
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.addChoosableFileFilter(new FileNameExtensionFilter("text Documents", "txt"));
fc.setAcceptAllFileFilterUsed(true);
int returnVal=fc.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String path= fc.getSelectedFile().getAbsolutePath();
System.out.println(path);
String temp = path.replaceAll("\"", "/");
System.out.println(temp);
}
}
UFF功課...... – Apurva
你替換「字符,而不是\字符。改爲'「\\\\」'而不是'「\」「'。您需要三個轉義反斜槓來迎合replaceAll接受regex參數的事實。 – JonK