Groovy:我如何在字符串中包含反斜杠而不逃脱? [英] Groovy: How can I include backslashes inside a string without escaping?
问题描述
C:\dev\我想在我的groovy程序中使用下面的字符串字面值,而不必转义反斜杠。用户名
以下是我到目前为止所尝试的内容:
行情)& GStrings(双引号)
$ $ $ $ $ $ $ def aString ='C:\dev\username'
def aGString =C: \dev\username
- 不起作用,因为\具有特殊含义并且用于转义其他字符
- 我最终不得不使用另一个\
def s ='C:\\dev\\username'
Slashy String& Dollar Slashy String
适用于某些字符串,如下所示:
def slashy = / C:\windows\system32/
def dollarSlashy = $ / C:\windows\system32 / $
但它将\u解释为具有特殊含义(以下不起作用):
def s1 = / C:\dev\username /
def s2 = $ / C:\dev\username / $
- Groovy:未找到四位数十六进制字符代码
哇,另一个将Windows文件路径放入斜杠字符串的问题。很好的接收。我之前遇到的问题是在路径中包含尾部反斜杠,例如 / C:\路径\ /
,导致意外的char:0xFFFF
错误。
无论如何,为了答案的缘故,鉴于Windows路径不区分大小写,为什么不一次性利用它?
def s = / C:\DEV\USERNAME /
\u
unicode字符转义序列是区分大小写的。
I want to use the following string literal inside my groovy program without having to escape the backslashes:
C:\dev\username
Here is what I have tried so far:
String (Single Quotes) & GStrings (Double Quotes)
def aString = 'C:\dev\username' def aGString = "C:\dev\username"
- Doesn't work because \ has special meaning and is used to escape other characters
- I end up having to escape \ with another \
def s = 'C:\\dev\\username'
Slashy String & Dollar Slashy String
Works for some strings, like the following
def slashy = /C:\windows\system32/ def dollarSlashy = $/C:\windows\system32/$
But it interprets \u as having special meaning (the following don't work):
def s1 = /C:\dev\username/ def s2 = $/C:\dev\username/$
- Groovy:Did not find four digit hex character code
Wow, another gotcha with putting Windows files paths in slashy strings. Nice catch. The gotcha I've encountered before was including a trailing backslash on the path, e.g. /C:\path\/
, which results in an unexpected char: 0xFFFF
error.
Anyways, for the sake of an answer, given that Windows paths are case insensitive, why not exploit it for once?
def s = /C:\DEV\USERNAME/
The \u
unicode character escape sequence is case sensitive.
这篇关于Groovy:我如何在字符串中包含反斜杠而不逃脱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!