遇到.split(“\\”)方法有问题。 [英] Having trouble with a .split("\\") method.
问题描述
再次问好。我正在尝试将ArrayList< String>作为输入。并利用String的.spit(分隔符)方法将其转换为String [] []。我收到了一些错误(我会发布代码并将错误降低)。
这里有一个简单的例子:
>
您需要四个反斜杠来匹配\,因为它是一个特殊的特殊字符。
您需要四个反斜杠来匹配\,因为它是一个特殊的特殊字符。
只是详细说明r025198x的非常正确和非常简洁的回复:
假设你要拆分单反斜杠;你必须用
另一个反斜杠来逃避它(因为r035198x已经正确写了),反斜杠
是正则表达式编译器的一个特殊字符。
如果要将该正则表达式作为文字字符串提供,则必须通过Java编译器获得
,但javac认为反斜杠字符为
特殊字符,所以基本上你想得到两个反斜杠
javac;每个需要一个额外的反斜杠:
展开 | 选择 | Wrap | 行号
只是详细说明r025198x的非常正确和非常简洁的回复:
假设你想要拆分一个反斜杠;你必须用
另一个反斜杠来逃避它(因为r035198x已经正确写了),反斜杠
是正则表达式编译器的一个特殊字符。
如果要将该正则表达式作为文字字符串提供,则必须通过Java编译器获得
,但javac认为反斜杠字符为
特殊字符,所以基本上你想得到两个反斜杠
javac;每个需要一个额外的反斜杠:
展开 | 选择 | Wrap | 行号
Hello again. I''m trying to take as an input an ArrayList<String> and utilize String''s .spit(delimiter) method to turn that into a String[][]. I''m getting some kind of error though (I''ll post the code and error lower down).
Here''s a simple example:
Expand|Select|Wrap|Line Numbers解决方案You need four backslashes to match \ because it is a special-special character.
You need four backslashes to match \ because it is a special-special character.Just to elaborate on r025198x''s awfully correct and awfully terse reply:
Suppose you want to split on a single backslash; you have to escape it with
another backslash because (as r035198x already correctly wrote), the backslash
is a special character for the regular expression compiler.
If you want to supply that regular expression as a literal String, you have to get
it past the Java compiler too but javac considers a backslash character as a
special character as well, so basically you want to get two backslashes past
javac; that takes one extra backslash each:
Expand|Select|Wrap|Line Numbers
Just to elaborate on r025198x''s awfully correct and awfully terse reply:
Suppose you want to split on a single backslash; you have to escape it with
another backslash because (as r035198x already correctly wrote), the backslash
is a special character for the regular expression compiler.
If you want to supply that regular expression as a literal String, you have to get
it past the Java compiler too but javac considers a backslash character as a
special character as well, so basically you want to get two backslashes past
javac; that takes one extra backslash each:
Expand|Select|Wrap|Line Numbers
这篇关于遇到.split(“\\”)方法有问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文登录 关闭
扫码关注1秒登录发送“验证码”获取 | 15天全站免登陆