「Java小方法」 判斷一個字串是否為空串,全空格,或者null
阿新 • • 發佈:2019-02-09
前後端進行互動,任何一個都介面少不了資料校驗,所以,如果事先寫好一個方法,能便捷的判斷入參是否符合要求,會省不少的事情。
以下就是用於判斷傳入字串是否為為空,空串或者全空格的方法:
/ ** * 判斷字串是否為null 或者空串或者空格串 * * @param strings 傳入若干字串 * @return遍歷傳入的字串,若有空則返回true * / public static boolean isEmpty(String ...strings){ for(String str:strings){ if(str == null || str.trim().length()== 0) return true; } return false; }
英文的以下增強版,可以返回為空的字串(若傳入三個字串都為空,則會先返回回一個為空的字串並返回)
/ ** * 判斷字串是否為空或者空串或者空格串 * @引數字串傳入若干字串 * @返回遍歷傳入的字串,若有空則返回為空的字串 * / public static String isEmpty(String ... strings){ for(String str:strings){ if(str == null || str.trim().length()== 0) return str ; } return null; }
可以很方便的判斷並且取出其中為空的字串