Java 在 ^(插入符号?)上拆分不起作用,这是一个特殊字符吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10695104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:08:23  来源:igfitidea点击:

Java split on ^ (caret?) not working, is this a special character?

javasplit

提问by art vanderlay

In Java, I am trying to split on the ^character, but it is failing to recognize it. Escaping \^throws code error.

在 Java 中,我试图拆分^字符,但无法识别它。转义\^会引发代码错误。

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); 

回答by wkl

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 \nfor newlines.

您将需要使用"\\^". 需要双斜线来转义\,否则 Java 的编译器会认为您正在尝试\^在字符串中使用特殊序列,类似于\n换行符。

\^is not a special escape sequence though, so you will get compiler errors.

\^不是一个特殊的转义序列,所以你会得到编译器错误。

In short, use "\\^".

简而言之,使用"\\^".

回答by Dean Povey

The ^ matches the start of string. You need to escape it, but in this case you need to escape it so that the regular expression parser understands which means escaping the escape, so:

^ 匹配字符串的开头。你需要转义它,但在这种情况下你需要转义它,以便正则表达式解析器理解这意味着转义,所以:

String splitChr = "\^";
...

should get you what you want.

应该得到你想要的。

回答by Pavel Veller

String.split()accepts a regex. The ^sign is a special symbol denoting the beginning of the input sequence. You need to escape it to make it work. You were right trying to escape it with \but it's a special character to escape things in Java strings so you need to escape the escape character with another \. It will give you:

String.split()接受正则表达式。该^标志是一个特殊符号,表示输入序列的开始。你需要逃避它才能让它工作。你试图转义它是正确的,\但它是一个特殊的字符来转义 Java 字符串中的东西,所以你需要用另一个\. 它会给你:

\^

回答by Basilio German

use "\\^". Use this example as a guide:

使用"\\^". 使用此示例作为指导:

    String aryToSplit = "word1^word2";
    String splitChr = "\^";
    String[] fmgStrng = aryToSplit.split(splitChr); 
    System.out.println(fmgStrng[0]+","+fmgStrng[1]);

It should print "word1,word2", effectively splitting the string using "\\^". The first slash is used to escape the second slash. If there were no double slash, Java would think ^ was an escape character, like the newline "\n"

它应该打印“word1,word2”,使用"\\^". 第一个斜线用于转义第二个斜线。如果没有双斜线,Java 会认为 ^ 是一个转义字符,就像换行符一样"\n"

回答by Karuna

None of the above answers makes no sense. Here is the right explanation.

以上答案都没有任何意义。这是正确的解释。

  1. As we all know, ^ doesn't need to be escaped in Java String.
  2. As ^ is special charectar in RegulalExpression , it expects you to pass in \^
  3. How do we make string \^ in java? Like this String splitstr = "\\^";
  1. 众所周知, ^ 不需要在 Java String 中转义。
  2. 由于 ^ 是 RegularExpression 中的特殊字符,它希望您传入 \^
  3. 我们如何在java中制作字符串\^?像这样 String splitstr = "\\^";