Can anyone help me find the LastIndexOf '\' in a string in java? I wrote the code as:
int i = app.lastIndexOf('\');
app
is my string, but there is a error as "Invalid character constant". I tried using double quotes but still no use. Can anyone help me on this?
The lastIndexOf() method returns the index (position) of the last occurrence of a specified value in a string. The lastIndexOf() method searches the string from the end to the beginning. The lastIndexOf() method returns the index from the beginning (position 0).
Java String indexOf() Method The indexOf() method returns the position of the first occurrence of specified character(s) in a string. Tip: Use the lastIndexOf method to return the position of the last occurrence of specified character(s) in a string.
indexOf() method returns the position of the first occurrence of a specified value in a string. string. lastIndexOf() method returns the position of the last occurrence of a specified value in a string.
The \
character is used to escape other characters, in order to print special characters.
For example, '\n'
is the "newline" character, '\t'
is the "tab" character, '\''
is the literal '
, etc.
In order to use a \
in a string, you have to escape it: \\
.
Use app.lastIndexOf('\\');
You have to escape \
as \\
in java Strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With