2015-10-14
7:03 PM
String.trim(); 是一個可以將字串前後空白刪除的方法
而若只想要刪除字串左邊/開頭的空白(也就是trim left)
或是只想要刪除字串右邊/結尾的空白(也就是trim right)
就可以使用 Apache Commons Lang 提供的 StringUtils 達成這個效果
程式碼範例
- /*
- * Gradle dependencies
- * compile 'org.apache.commons:commons-lang3:3.4'
- */
- import org.apache.commons.lang3.StringUtils;
- // 移除字串開頭空白 結果將印出 "Yeah! "
- StringUtils.stripStart( " Yeah! ", " " );
- // 移除字串結尾空白 結果將印出 " Yeah!"
- StringUtils.stripEnd( " Yeah! ", " " );
- // 且不限於刪除空白字元 結果將印出 "120"
- StringUtils.stripEnd( "120.00", ".0" );
各項資料連結
Apache Commons Lang
Apache Commons Lang Maven Repository
No comments:
Post a Comment