0
点赞
收藏
分享

微信扫一扫

最大字符串长度

止止_8fc8 2024-08-08 阅读 40

 

/**
 * @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;
    }
}
举报

相关推荐

0 条评论