java基础知识

一、三元运算符:

  条件表达式?表达式1:表达式2

  条件表达式成立时返回表达式1,否则返回表达式2

  应用:去除左右空格

package besttest.day01;

public class RTrimTest {
    /*
    删除右空格
     */
    public static String rTrim(String s){
        char[] val = s.toCharArray();
        int len = s.length();
        int index = 0;
        while (len>0 && val[len-1]<=' '){
            len--;
        }
        String result = len<s.length()?s.substring(0,len):s;
        return result;
    }

    public static void main(String[] args) {
        String s = "    abcdef    ";
        String result = rTrim(s);
        System.out.println(result);
    }
}
View Code
原文地址:https://www.cnblogs.com/wangkc/p/10940216.html