- /**
- * 字符串长度截取测试
- * Created by Administrator on 2014/7/7.
- */
- public class StringTest
- {
- public static void main(String[] args)
- {
- String str = "我叫瑞恩,您好!我要做一个字符串截取测试,我的电话是:123456789,你没事可以打给我,我们约会!";
- System.out.println(str.length());
- System.out.println(str.getBytes().length);
- int len = str.length(), maxLen = 60, temp = 0, i = 0;
- for(; i < len && temp <= maxLen; i++)
- {
- char c = str.charAt(i);
- int charLen = String.valueOf(c).getBytes().length;
- temp += charLen;
- System.out.println(c + ", index=" + i + ", curentIndex=" + temp);
- }
- str = str.substring(0, i-1);
- System.out.println(str.length());
- System.out.println(str.getBytes().length);
- System.out.println("[" + str + "]");
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2108201513509.html
来源: http://www.codesnippet.cn/detail/2108201513509.html