java字符轉義和替換

String oaFilePath = "F:\\work\\oa.xml";
        String message = getFileString(oaFilePath);
        Document doc = XmlUtils.parseText(message);
        Element rootElement = doc.getRootElement();
        String filePath = rootElement.element("Projects").
            element("ProjectRow").element("ProjectItem")
            .element("StorageUrl").getTextTrim();
        //java裏面\是轉義字符  \\前面一個就是轉義,就等於一個\ 。  \\\\ 四個轉義下不就是\\    
        filePath = filePath.replaceAll("\\\\", "/");
        //另一種方法  filePath.replace('\\', '/'); 可以實現同一種效果

        //share2是要替換的值  tobi3爲替換後的值
        filePath = filePath.replace("share2", "tobi3");
        System.out.println(filePath);

轉義前後這裏寫圖片描述

發佈了30 篇原創文章 · 獲贊 24 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章