为什么JSP中的LastIndexof函数没有给字符串的黑色斜线给出确切的索引值


<!DOCTYPE html>
<html>
<body>
<p>Click the button to locate where in the string a specifed value occurs.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
    var pattern = "\";
    var str = "C:filepathcontactlist.txt";
    var n = str.lastIndexOf(pattern)+1;
    document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>

异常值为12,但给出0,因为LastIndexof Fuction返回-1。当我用双重斜杠替换STR变量中的单斜杠(" C: filepath filepath contactList.txt" ),它给出了12个。

为什么 LastIndexof 当我在str中使用单个向后斜线时给出-1吗?

我是否使用双重向后斜杠模式正确搜索 str> str

中的单个向后斜线

字符串中使用后斜切字符来逃避字符串元容器,例如引号或后斜线本身,以及五个特殊的"控制"字符(n,r,r,f,f,t,t,和b)。当在任何其他字符之前使用时,忽略了后斜线。因此,当您仅使用单个后斜切构建该字符串时,结果实际字符串值将不包括任何后斜切。

最新更新