[英]Manipulate the special character '\' in a JAVA string
I have this String : 我有这个String:
pathfileshtml.get(b) = C:\Users\Netrill\workspace\find and fix\JAS_DOC\JAS\PRD_15M\JAS_JAE\JAS_JAE\JAS_JAE_0.1.html
I used this : 我用这个:
String cartella = pathfileshtml.get(b).substring(0,pathfileshtml.get(b).lastIndexOf("/"));
the Exception
is String index of range -1
in SO Windows, but it' s ok on MAC. 在SO Windows中Exception
是String index of range -1
,但是在MAC上是可以的。 I need now the Windows version . 我现在需要Windows版本。 How i can manipulate the character '\\' in a JAVA string on Windows? 我如何在Windows的JAVA字符串中操纵字符'\\'? Thanks 谢谢
The \\
character is called an escape character. \\
字符称为转义字符。
It is used for adding characters such as \\n
and \\b
. 它用于添加\\n
和\\b
等字符。
If you want to use \\
you must type \\\\
to have it escape itself. 如果要使用\\
,则必须键入\\\\
以使其自身转义。
I think that you are taking the wrong approach to this. 我认为您对此采取了错误的方法。
Assuming that you are on a Windows machine trying to manipulate a Windows pathname, you should be doing something like this to parse the pathname string as a Path
object: 假设您在Windows机器上尝试操纵Windows路径名,则应该执行以下操作将路径名字符串解析为Path
对象:
Path path = FileSystems.getDefault().getPath(pathString);
Once you have the Path
you can perform a variety of operations. 拥有Path
您可以执行各种操作。 For instance, to get the last name in the path: 例如,要获取路径中的姓氏:
String filename = path.getFileName();
Using the Path
class avoids the need to parse the pathname yourself, and it (largely) insulates you from platform specific pathname syntax issues. 使用Path
类避免了自己解析路径名的需要,并且它(很大程度上)使您免受特定于平台的路径名语法问题的困扰。 (The code above should work on Mac OSX, UNIX, Linux and so on ... as well as Windows.) (以上代码应在Mac OSX,UNIX,Linux等……以及Windows上运行。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.