如何使用java替换反斜杠来转发斜杠? [英] How to replace backward slash to forward slash using java?
问题描述
我正在将CSV文件导入MySQL数据库。这可以使用 java.mysql
支持文件路径中的正斜杠。如果用户提供路径
I'm importing a CSV file to MySQL database. This can be done using java.mysql
support for forward slash in file path. If user gives the path
c:\upload\date\csv\sample.csv
MySQL不支持这种类型的路径模式。我想在路径中搜索反斜杠并用正斜杠替换它们,为此:
MySQL doesn't support this type of path pattern. I want to search for backslashes in the path and replace them with a forward slash, to give this:
c:/upload/date/csv/sample.csv
这是怎么做的?
推荐答案
在java中,使用:
str = str.replace("\\", "/");
注意替换的正则表达式版本,即 replaceAll(这里不需要
; replace()
仍会替换所有出现的搜索字词,但会搜索 literal 字符串,而不是正则表达式匹配。
Note that the regex version of replace, ie replaceAll()
, is not required here; replace()
still replaces all occurrences of the search term, but it searches for literal Strings, not regex matches.
这篇关于如何使用java替换反斜杠来转发斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!