开发者

Ignoring octalescape characters in a string

开发者 https://www.devze.com 2023-01-23 16:32 出处:网络
Here is a sample String开发者_如何转开发 \"one/two/three\\123today\" that i get from an unknown source i.e i cannot change the format of the input string that i get.

Here is a sample String开发者_如何转开发 "one/two/three\123today" that i get from an unknown source i.e i cannot change the format of the input string that i get. I need to get the sub-string after the backslash i.e 123today

Here the \123 is being considered as an octal escape. I tried splitting it as a character sequence, but this considers the octal escape as a character.

I am writing the code in java. How do i go about it?


The answer is very simple.

If you want your Java program to contain a Java String literal containing the character sequence '\', '1', '2', '3', you MUST write it as "...\\123..." in your source code.

For example:

String testInput = "one/two/three\\123today";
int pos = test.indexOf("\\123");

However, backslash escaping is only relevant to Java string (or character) literals in your source code. If your program reads the String from some file (for example), or if it assembles the String in some way that doesn't involve String or character literals, no escaping is required in the source file, or whatever. For example:

char backslash = (char) 92;
String testInput = "one/two/three" + backslash + "123today";
int pos = test.indexOf(backslash + "123");

or

String input = ... // read a file that contains the sequence '\', '1', '2', '3'
int pos = test.indexOf("\\123"); // search for that sequence

(Aside: some programming languages provide alternative String literal syntaxes that mean that you can dispense with escaping. Java does not. End of story.)

Here the \2 is being considered as an octal escape by eclipse.

For the record, it the Java Language Specification that defines this. Eclipse is just (correctly) implementing the Java Language Specification.


The string "one/two/three\123today" is exactly the same as "one/two/threeStoday". If you want to split on an 'S' character, you can do that, but there’s no way to tell whether a character was encoded directly or via an escape sequence.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号