For example, from the string "aaaaaasd" display only "sd"

  • 2
    String.substring () something does not suit you? - Vladimir Martyanov
  • If my answer suits you, mark it correct by clicking on the check mark. - Flippy

1 answer 1

String str = "aaaaaasd"; String new_str = str.substring(a,b); 

Where a is the beginning index, b is the end index. If you want to cut from some sign to the end, then specify only a. In your case, you need to

 String new_str = str.substring(str.length()-2); System.out.println(new_str); 

Ie cut the last two characters and display them

  • It would be nice to check that there are at least 2 characters in the string, otherwise you can get an exception. - Roman