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
我正在导入一个CSV文件到MySQL数据库。这可以使用java实现。mysql支持文件路径的正斜杠。如果用户给出路径
c:\upload\date\csv\sample.csv
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:
MySQL不支持这种路径模式。我想在路径中搜索反斜杠,然后用一个正斜杠替换它们,得到如下结果:
c:/upload/date/csv/sample.csv
How is that done?
是怎么做到的呢?
3 个解决方案
#1
44
In java, use this:
在java中,使用这个:
str = str.replace("\\", "/");
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.
注意,这里不需要regex版本的replace ();replace()仍然替换搜索词出现的所有内容,但它搜索的是文字字符串,而不是正则表达式匹配。
#2
8
The String.replace(CharSequence, CharSequence) example provided by @PaulPRO and @Bohemian will work, but its better to use the String.replace(char, char) version. Slightly faster. Though you won't have a noticeable speed difference, its better to do such optimisations where possible.
的字符串。由@PaulPRO和@Bohemian提供的替换(CharSequence, CharSequence)示例可以工作,但是最好使用字符串。替换(字符,字符)版本。稍快。虽然您不会有明显的速度差异,但最好在可能的地方做这样的优化。
String replacedStr = str.replace('\\', '/');
#3
5
If you have:
如果你有:
String s = "c:\\upload\\date\\csv\\sample.csv";
上传字符串s = " c:\ \ \ \ \ \ csv \ \ sample.csv日期”;
In Java, you can just use:
在Java中,您可以使用:
s = s.replace("\\", "/");
s =。替换(“\ \”,“/”);
Which will make s equal to:
这将使s等于:
c:/upload/date/csv/sample.csv
c:/上传/日期/ csv / sample.csv
#1
44
In java, use this:
在java中,使用这个:
str = str.replace("\\", "/");
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.
注意,这里不需要regex版本的replace ();replace()仍然替换搜索词出现的所有内容,但它搜索的是文字字符串,而不是正则表达式匹配。
#2
8
The String.replace(CharSequence, CharSequence) example provided by @PaulPRO and @Bohemian will work, but its better to use the String.replace(char, char) version. Slightly faster. Though you won't have a noticeable speed difference, its better to do such optimisations where possible.
的字符串。由@PaulPRO和@Bohemian提供的替换(CharSequence, CharSequence)示例可以工作,但是最好使用字符串。替换(字符,字符)版本。稍快。虽然您不会有明显的速度差异,但最好在可能的地方做这样的优化。
String replacedStr = str.replace('\\', '/');
#3
5
If you have:
如果你有:
String s = "c:\\upload\\date\\csv\\sample.csv";
上传字符串s = " c:\ \ \ \ \ \ csv \ \ sample.csv日期”;
In Java, you can just use:
在Java中,您可以使用:
s = s.replace("\\", "/");
s =。替换(“\ \”,“/”);
Which will make s equal to:
这将使s等于:
c:/upload/date/csv/sample.csv
c:/上传/日期/ csv / sample.csv