在android中使用问号作为字符串

时间:2022-02-08 22:24:30

Im trying to use a question mark as a variable for a string.

我试图使用问号作为字符串的变量。

I've tried...

strings.xml

<string name="questionMark">\?</string>

.class

String questionMark;
questionMark = getResources().getString(R.string.questionMark);
String delim4 = (questionMark);

This causes a fource close regex error.

这会导致fource关闭正则表达式错误。

and

String delim4 = (\?);

This gets an error Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )

这会出现错误无效的转义序列(有效转义序列为\ b \ t \ n \ f \ r \“\ \ \ \)

and also

I've tried putting 2 backslashes in front of it

我试过在它前面放两个反斜杠

String delim4 =(\\?)
System.out.println("delim "+ delim4);

But that just escapes the second slash and sometimes force closes as well. the output for that was

但这只是逃脱了第二次斜线,有时力也会关闭。那个输出是

delim \?

Can any tell me how to put in the question mark as the string. I'm using it as variable to spit a string. The String Im splitting can not be changed.

可以告诉我如何将问号作为字符串。我用它作为变量来吐出一个字符串。 String Im拆分无法更改。

plz help

Edit added split code

编辑添加的拆分代码

                               if (FinishedUrl.contains(questionMark)){ 


                                    String delim3 = (".com/");
                                    String[] parts3 = FinishedUrl.split(delim3);
                                    String  JUNK3= parts3[0];
                                    String fIdStpOne = parts3[1];

                                    String fIdStpTwo = fIdStpOne.replaceAll("=#!/","");

                                    String delim4 = (questionMark);
                                    String[] parts4 = fIdStpTwo.split(delim4);
                                    String  fIdStpThree= parts3[0];
                                    String JUNK4 = parts3[1];


                                    FId = fIdStpThree;
                               }

4 个解决方案

#1


5  

As pointed out by user laalto, ? is a meta-character in regex. You must work around that.

正如用户laalto指出的那样,?是正则表达式中的元字符。你必须解决这个问题。

Let's see what's happening here. Firstly, some ground rules:

让我们看看这里发生了什么。首先,一些基本规则:

`?` is not a special character in Java
`?` is a reserved character in regex

This entails:

String test = "?";     // Valid statement in java, but illegal when used as a regex

String test = "\?";    // Illegal use of escape character

Why is the second statement wrong? Because we are trying to escape a character that isn't special (in Java). Okay, we'll get back to this.

为什么第二个陈述错了?因为我们试图逃避一个不特殊的角色(在Java中)。好的,我们会回到这个。

Now, for the split(String) method, we need to escape the ? - it being a meta-character in regex. So, we need \? for the regex.

现在,对于split(String)方法,我们需要转义? - 它是正则表达式中的元字符。那么,我们需要\?为正则表达式。

Coming back to the string, how do we get \?? We need to escape the \(backslash) - not the question mark!

回到字符串,我们怎么得到\ ??我们需要逃避\(反斜杠) - 不是问号!

Here's the workflow:

这是工作流程:

String delim4 = "\\?";

This statement gives us \? - it escapes the \(backslash).

这句话给了我们\? - 它逃脱了\(反斜杠)。

String[] parts4 = fIdStpTwo.split(delim4);

This lets us use \? as a regex in the split() method. Since delim4 is being passed as a regex, \? is used as ?. Here, the prefix \ is used to escape ?.

这让我们用\?作为split()方法中的正则表达式。由于delim4作为正则表达式传递,\?用作?。这里,前缀\用于转义?


Your observations:

String delim4 = (\?);

This gets an error Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )

这会出现错误无效的转义序列(有效转义序列为\ b \ t \ n \ f \ r \“\ \ \ \)

I covered this above. You are escaping ? at the java level - but it isn't a special character and needs no escaping - hence the error.

我在上面介绍了这个。你在逃避?在java级别 - 但它不是一个特殊字符,不需要转义 - 因此错误。

String delim4 =(\\?)
System.out.println("delim "+ delim4);

But that just escapes the second slash and sometimes force closes as well. the output for that was

但这只是逃脱了第二次斜线,有时力也会关闭。那个输出是

delim \?

This is what we want. It is easier to think of this as a two stage process. The first stage deals with successfully placing a \(backslash) in front of the ?. In the second stage, regex finds that the ? has been prefixed by a \ and uses ? as a literal instead of a meta-character.

这就是我们想要的。将其视为两阶段过程更容易。第一阶段处理成功地在?前面放置\(反斜杠)。在第二阶段,正则表达式发现?已被前缀为\并使用?作为文字而不是元字符。


And here's how you can place the regex in your res/values/strings.xml:

以下是如何将正则表达式放在res / values / strings.xml中:

<string name="questionMark">\\?</string>

By the way, there's another option - not something I use on a regular basis these days - split() works just fine.

顺便说一句,还有另一种选择 - 这些天我不定期使用 - split()工作正常。

You can use StringTokenizer which works with delimiters instead of regex. Afaik, any literal can be used as a delimiter. So, you can use ? directly:

您可以使用StringTokenizer,它使用分隔符而不是正则表达式。 Afaik,任何文字都可以用作分隔符。那么,你可以使用?直:

StringTokenizer st = new StringTokenizer(stringToSplit, "?");

while (st.hasMoreTokens()) {
    // Use tokens
    String token = st.nextToken();
}

#2


1  

Easiest way is to quote or backslash them:

最简单的方法是引用或反斜杠:

<string name="random">"?"</string>
<string name="random">\?</string>

#3


0  

The final code.

最后的代码。

String startDelim = ("\\?");
String realDelim = (startDelim);
String[] parts4 = fIdStpOne.split(realDelim);
String  fIdStpTwo= parts4[0];
String JUNK4 = parts4[1];

#4


-1  

Normally you'd just put it literally, like

通常你只是把字面意思,就像

String q = "?";

However, you say you're using it to split a string. split() takes a regular expression and ? is a metacharacter in a regex. To escape it, add a backslash in front. Backslash is a special character in Java string literals so it needs to be escaped, too:

但是,你说你用它来分割字符串。 split()采用正则表达式?是正则表达式中的元字符。要逃避它,请在前面添加反斜杠。反斜杠是Java字符串文字中的一个特殊字符,因此它也需要进行转义:

String q = "\\?";

#1


5  

As pointed out by user laalto, ? is a meta-character in regex. You must work around that.

正如用户laalto指出的那样,?是正则表达式中的元字符。你必须解决这个问题。

Let's see what's happening here. Firstly, some ground rules:

让我们看看这里发生了什么。首先,一些基本规则:

`?` is not a special character in Java
`?` is a reserved character in regex

This entails:

String test = "?";     // Valid statement in java, but illegal when used as a regex

String test = "\?";    // Illegal use of escape character

Why is the second statement wrong? Because we are trying to escape a character that isn't special (in Java). Okay, we'll get back to this.

为什么第二个陈述错了?因为我们试图逃避一个不特殊的角色(在Java中)。好的,我们会回到这个。

Now, for the split(String) method, we need to escape the ? - it being a meta-character in regex. So, we need \? for the regex.

现在,对于split(String)方法,我们需要转义? - 它是正则表达式中的元字符。那么,我们需要\?为正则表达式。

Coming back to the string, how do we get \?? We need to escape the \(backslash) - not the question mark!

回到字符串,我们怎么得到\ ??我们需要逃避\(反斜杠) - 不是问号!

Here's the workflow:

这是工作流程:

String delim4 = "\\?";

This statement gives us \? - it escapes the \(backslash).

这句话给了我们\? - 它逃脱了\(反斜杠)。

String[] parts4 = fIdStpTwo.split(delim4);

This lets us use \? as a regex in the split() method. Since delim4 is being passed as a regex, \? is used as ?. Here, the prefix \ is used to escape ?.

这让我们用\?作为split()方法中的正则表达式。由于delim4作为正则表达式传递,\?用作?。这里,前缀\用于转义?


Your observations:

String delim4 = (\?);

This gets an error Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )

这会出现错误无效的转义序列(有效转义序列为\ b \ t \ n \ f \ r \“\ \ \ \)

I covered this above. You are escaping ? at the java level - but it isn't a special character and needs no escaping - hence the error.

我在上面介绍了这个。你在逃避?在java级别 - 但它不是一个特殊字符,不需要转义 - 因此错误。

String delim4 =(\\?)
System.out.println("delim "+ delim4);

But that just escapes the second slash and sometimes force closes as well. the output for that was

但这只是逃脱了第二次斜线,有时力也会关闭。那个输出是

delim \?

This is what we want. It is easier to think of this as a two stage process. The first stage deals with successfully placing a \(backslash) in front of the ?. In the second stage, regex finds that the ? has been prefixed by a \ and uses ? as a literal instead of a meta-character.

这就是我们想要的。将其视为两阶段过程更容易。第一阶段处理成功地在?前面放置\(反斜杠)。在第二阶段,正则表达式发现?已被前缀为\并使用?作为文字而不是元字符。


And here's how you can place the regex in your res/values/strings.xml:

以下是如何将正则表达式放在res / values / strings.xml中:

<string name="questionMark">\\?</string>

By the way, there's another option - not something I use on a regular basis these days - split() works just fine.

顺便说一句,还有另一种选择 - 这些天我不定期使用 - split()工作正常。

You can use StringTokenizer which works with delimiters instead of regex. Afaik, any literal can be used as a delimiter. So, you can use ? directly:

您可以使用StringTokenizer,它使用分隔符而不是正则表达式。 Afaik,任何文字都可以用作分隔符。那么,你可以使用?直:

StringTokenizer st = new StringTokenizer(stringToSplit, "?");

while (st.hasMoreTokens()) {
    // Use tokens
    String token = st.nextToken();
}

#2


1  

Easiest way is to quote or backslash them:

最简单的方法是引用或反斜杠:

<string name="random">"?"</string>
<string name="random">\?</string>

#3


0  

The final code.

最后的代码。

String startDelim = ("\\?");
String realDelim = (startDelim);
String[] parts4 = fIdStpOne.split(realDelim);
String  fIdStpTwo= parts4[0];
String JUNK4 = parts4[1];

#4


-1  

Normally you'd just put it literally, like

通常你只是把字面意思,就像

String q = "?";

However, you say you're using it to split a string. split() takes a regular expression and ? is a metacharacter in a regex. To escape it, add a backslash in front. Backslash is a special character in Java string literals so it needs to be escaped, too:

但是,你说你用它来分割字符串。 split()采用正则表达式?是正则表达式中的元字符。要逃避它,请在前面添加反斜杠。反斜杠是Java字符串文字中的一个特殊字符,因此它也需要进行转义:

String q = "\\?";