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的编译器会认为您正在尝试使用特殊的 \ ^
字符串中的序列,类似于换行符的 \ n
。
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 \n
for newlines.
\ ^
虽然不是特殊的转义序列,但是你会遇到编译错误。
\^
is not a special escape sequence though, so you will get compiler errors.
简而言之,使用 \\ ^
。
这篇关于Java拆分^(插入?)不工作,这是一个特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!