Java 在 ^(插入符号?)上拆分不起作用,这是一个特殊字符吗? [英] Java split on ^ (caret?) not working, is this a special character?
问题描述
在 Java 中,我试图在 ^
字符上拆分,但无法识别它.转义 ^
会引发代码错误.
In Java, I am trying to split on the ^
character, but it is failing to recognize it. Escaping ^
throws code error.
这是一个特殊字符还是我需要做其他事情才能让它识别它?
Is this a special character or do I need to do something else to get it to recognize it?
String splitChr = "^";
String[] fmgStrng = aryToSplit.split(splitChr);
推荐答案
^
是 Java 正则表达式中的一个特殊字符 - 表示匹配输入的开头".
The ^
is a special character in Java regex - it means "match the beginning" of an input.
您需要使用 "\^"
对其进行转义.需要双斜线来转义 ,否则 Java 的编译器会认为您正在尝试在字符串中使用特殊的
^
序列,类似于
用于换行.
You will need to escape it with "\^"
. The double slash is needed to escape the , otherwise Java's compiler will think you're attempting to use a special
^
sequence in a string, similar to
for newlines.
^
不是一个特殊的转义序列,所以你会得到编译器错误.
^
is not a special escape sequence though, so you will get compiler errors.
简而言之,使用"\^"
.
这篇关于Java 在 ^(插入符号?)上拆分不起作用,这是一个特殊字符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!