Home java What does and why do you need the string.lastindexof () method

What does and why do you need the string.lastindexof () method

Author

Date

Category

Why do you need lastindexof () and what is the meaning of its use? (Preferably with examples).

I read somewhere that this method returns the number of the last matched position with the specified index, but I still do not understand anyway.


Answer 1, Authority 100%

This method (Public int Lastindexof (String Str) ) gives the sequence number of the symbol on which your desired text begins. For example:

string str = "Hello Planet Earth, You are A Great Planet.";
int n = str.lastindexof ("Earth");

will give you 13. Because the last time the word Earth in the text begins with 13 positions.

If you are looking for the word Planet, the result will not be 6, but 36.

string str = "Hello Planet Earth, You are A Great Planet.";
int n = str.lastindexof ("Planet");

For the word Planet occurs in the line 2 times and the last of which they start with the 36th position.

In the event that the element was not found, then the result will be -1.

But carefully – this method has 3 further overload:

  • Public Int Lastindexof (int CH)
    • Search for one single symbol
  • Public Int Lastindexof (int CH, Int Frondex)
    • Search for a single symbol Starting from the Symbol number fromIndex. The score occurs in the reverse order.
  • Public Int Lastindexof (String Str, Int Frondex)
    • Similar to the previous one. But there is not a single symbol search, but a whole line.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions