a- package demo.collection;
- public class TrimDemo {
- /**
- * 模拟trim功能
- */
- public static void main(String[] args) {
- String str = " sdf ";
- System.out.println(str);
- str = myTrim(str);
- System.out.println(str);
- String s = "0123456";
- System.out.println(s.substring(2,5));//左闭右开 234
- }
- private static String myTrim(String str) {
- char[] chs = str.toCharArray();
- int start = -1, end = chs.length ;
- while (start <= end && str.charAt(++start) == ' ');
- while (start <= end && str.charAt(--end) == ' ');
- return str.substring(start, end + 1);
- }
- }
复制代码
|
|