android TextUtils的使用
生活随笔
收集整理的這篇文章主要介紹了
android TextUtils的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天,簡單講講如何使用android自動的工具類TextUtils。
簡單列舉部分用法:
Log.d(TAG, "---------------------------------");//字符串拼接Log.d(TAG, TextUtils.concat("Hello", " ", "world!").toString());//判斷是否為空字符串Log.d(TAG, TextUtils.isEmpty("Hello") + "");//判斷是否只有數字Log.d(TAG, TextUtils.isDigitsOnly("Hello") + "");//判斷字符串是否相等Log.d(TAG, TextUtils.equals("Hello", "Hello") + "");//獲取字符串的倒序Log.d(TAG, TextUtils.getReverse("Hello", 0, "Hello".length()).toString());//獲取字符串的長度Log.d(TAG, TextUtils.getTrimmedLength("Hello world!") + "");Log.d(TAG, TextUtils.getTrimmedLength(" Hello world! ") + "");//獲取html格式的字符串Log.d(TAG, TextUtils.htmlEncode("<html>\n" +"<body>\n" +"這是一個非常簡單的HTML。\n" +"</body>\n" +"</html>"));//獲取字符串中第一次出現子字符串的字符位置Log.d(TAG, TextUtils.indexOf("Hello world!", "Hello") + "");//截取字符串Log.d(TAG, TextUtils.substring("Hello world!", 0, 5));//通過表達式截取字符串Log.d(TAG, TextUtils.split(" Hello world! ", " ")[0]);結果如下:
這其中重點講講如何使用TextUtils.isEmpty()。
? 是否為空字符 static boolean ?isEmpty(CharSequence str)?這個函數在我們判斷字符串為空時經常可以用到。
這里注意一點,空格返回的也是false。其實看看源碼就知道
/*** Returns true if the string is null or 0-length.* @param str the string to be examined* @return true if str is null or zero length*/public static boolean isEmpty(CharSequence str) {if (str == null || str.length() == 0)return true;elsereturn false;}
如果傳入是空格,字符串的長度不會為0,因此返回時false。為了判斷EditText輸入的是否為空字符串,可以將字符串先trim(),再傳入isEmpty,就能成功判斷了。
android TextUtils的使用就講完了。
就這么簡單。
總結
以上是生活随笔為你收集整理的android TextUtils的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android studio设置生成的r
- 下一篇: android 解决华为 Sliding