There is a String variable with two options:

 String string= "RUB 1234"; 

or

 String string= "RUB 1234 RUB 4567"; 

A condition is required to check if the second RUB is in this variable.

  • If only one RUB, then ...
  • Otherwise ...

I need the condition itself. Is it possible to realize this?

  • What should be the result for the first and second options? - default locale
  • If only one RUB, then ... Otherwise ... I need the condition itself - Artem Ilinsky
  • You can count the number of repetitions of a word with the help of regex and then check by count .. - entithat
  • Can I have a code sample? - Artem Ilinsky
  • @ Artem Ilinsky And how is the first RUB being sought? - MBo

2 answers 2

Using a regular expression, you can check if the RUB occurs more than once. For example:

 if(string.matches(".*RUB.*RUB.*")) { //есть два RUB } else { //один или ноль RUB } 

It is possible without regularizers, checking the first and last index:

 if(input.indexOf("RUB")==input.lastIndexOf("RUB")) { //не более одного RUB } else { //более одного RUB } 

    If the task is exactly the same and will not be expanded, then it is enough to run twice a suitable variant of indexOf

      public int indexOf(String str, int fromIndex) 

    from the zero position and after that found for the first time