gpt4 book ai didi

java - string.replaceAll 问题

转载 作者:搜寻专家 更新时间:2023-11-01 02:29:24 24 4
gpt4 key购买 nike

我只是想用 \\\\ 替换字符串 \\

下面是程序但是正在终止

String path="\\dctmadmin\\Human Resource\\Training\\Procedures\\Formalities\\Legalities\\Material";         

long start = System.currentTimeMillis();
// replace this string \\ with \\\\
String formatedPath = path.replaceAll("\\\\", "\\\\\\\\");
System.out.println(" string after formatting using replaceAll = "+formatedPath);
long end = System.currentTimeMillis();
System.out.println(" time take in milli seconds for String.replaceAll = "+Long.toString(end-start) );

请让我知道我做的错误。

最佳答案

对于不需要正则表达式功能的文字字符串替换,您应该使用 replace 而不是 replaceAll,因为它更简单、更高效。

// replace single backslash with double
String formatedPath = path.replace("\\", "\\\\");

关于java - string.replaceAll 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13191921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com