简体   繁体   English

string.replaceAll的问题

[英]Issue with string.replaceAll

I'm just trying to replace a string \\\\ with \\\\\\\\ 我只是想用\\\\\\\\替换字符串\\\\

Below is the program but it is terminating 以下是该计划,但它正在终止

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) );

Please let me know the mistake i was doing. 请告诉我我正在做的错误。

For a literal string replacement where you don't need the power of regular expressions you should use replace rather than replaceAll as it's simpler and more efficient. 对于不需要正则表达式功能的文字字符串替换,您应该使用replace而不是replaceAll因为它更简单,更高效。

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

Your real String contains only one \\. 你真正的String只包含一个\\。 Test 测试

System.out.println("\\dctmadmin\\Human Resource\\Training\\Procedures\\Formalities\\Legalities\\Material");

So your replaceAll is running OK if it returns double \\\\ when System.out.println("your string".replaceAll(...)); 所以当你的replaceAll在System.out.println(“你的字符串”.replaceAll(...))时返回double \\\\时,它运行正常;

Try as 试试吧

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));

System.out.println(" path  "+formatedPath);

in your string when you say \\ it actually means \\ escaped by another \\, thus it is replacing it correctly. 在你的字符串中,当你说\\它实际意味着\\由另一个\\转义时,因此它正确地替换它。

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

         System.out.println("String before: "+ path);
        long start = System.currentTimeMillis();
        // replace this string \\ with \\\\

          String formatedPath = path.replaceAll("\\\\", "\\\\\\\\");

          System.out.println(" string after formatting using replaceAll = "+formatedPath);

output i got 输出我得到了

   String before:  \dctmadmin\Human Resource\Training\Procedures\Formalities\Legalities\Material

     string after formatting using replaceAll = \\dctmadmin\\Human Resource\\Training\\Procedures\\Formalities\\Legalities\\Material
     time take in milli seconds for String.replaceAll = 2

You did right. 你没事。 In Java string \\\\ represents one backslash, in regex it is the non-String escape. 在Java中,字符串\\\\表示一个反斜杠,在正则表达式中它是非String转义符。

    String formatedPath = path.replaceAll("\\\\", "\\\\\\\\");
    System.out.println("path         = " + path);
    System.out.println("formatedPath = " + formatedPath);

gives

path         = \dctmadmin\Human Resource\Training\Pr...s\Form...s\L...s\Material
formatedPath = \\dctmadmin\\Human Resource\\Training\\Pr..s\\Form...s\\Material

Output of 输出

string after formatting using replaceAll = \\dctmadmin\\Human Resource\\Training\\Procedures\\Formalities\\Legalities\\Material

is correct. 是正确的。 It looks like it hasn't changed because you're discounting the fact that the '\\' in your original are escaped :) 看起来它没有改变,因为你正在打折原始中的'\\'被转义的事实:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM