/** * @author gyf * @ClassName Test * @Date 2024/8/3 17:22 * @Version V1.0 * @Description : */ public class Test { public static void main(String[] args) { String s1 = "Hello World "; System.out.println(getLength(s1)); String s2 = "fly me to the moon"; System.out.println(getLength(s2)); String s3 = "luccy is still joybys"; System.out.println(getLength(s3)); } public static int getLength(String str) { int length = 0; for (int i = str.length() - 1; i >= 0; i--) { if (str.charAt(i) != ' ') { length++; } else { break; } } return length; } }