简体   繁体   中英

How to get index of Escape Character in a String?

How to get index of Escape Character in a String?

String test="1234\567890";
System.out.println("Result : "+test.lastIndexOf("\\"));

Result i get: -1

Result i need: 4

Your original String doesn't contain \\ . Which means you are searching something which does not exist. Inorder to add \\ to your string. You have to escape while adding

String test="1234\\567890";
System.out.println("Result : "+test.lastIndexOf("\\"));

Should work.

在此输入图像描述

In your case look at the last line in the table .

I don't think you can get that because when you use an escape character is for java to interpret the following character in a special way. In another words, the escape character and the next character you see in the string are really one entity from the point of view of program being executed.

When you search for " \\\\ ", you are searching for the literal character ' \\ ' not the escape character.

Here you can see the difference: java fiddle

While \\5 is the character with code 0x5 (ENQ) , 5 is the character 0x35 . See the table .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM