简体   繁体   English

包含反斜杠的字符串中的替换函数

[英]replace function in string containing backslash \

I am trying to use the following code but it is giving me complilation error. 我正在尝试使用以下代码,但它给了我错误提示。 I need to replace \\ \\ (two backslashes) with \\ (one back slash )at all occurences. 我需要在所有情况下都用\\(一个反斜杠)替换\\\\(两个反斜杠)。

           if (principal.contains('\\\\') != -1)
                    {

                        {
                            principal = principal.replaceAll("\\\\", "\\");
                        }
                    }

Can anyone suggest something for this 有人可以为此提出建议吗

because '' for character , and you wrote String , so replace it like this: 因为''代表character,并且您编写了String ,所以将其替换为:

   '\\\\' --->"\\\\"

and contains return boolean not int 并且contains return boolean not int

principal.contains('\\\\')///will return true or false

and you not need for all this {} 并且您不需要所有这些{}

 if (principal.contains('\\\\') != -1)
    {
        {///delete this
          principal = principal.replaceAll("\\\\", "\\");
        }////and delete this
    }

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

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