字符串截取
来源:互联网 发布:php 个性化推荐系统 编辑:程序博客网 时间:2024/04/29 20:27
- /**
- * 字符串截取
- * @param str
- * @param bytes
- * @return
- */
- public static String strSplit(String str , int bytesLength) throws Exception{
- if(str == null){
- return null;
- }
- //转换成字符数组
- char[] sourceChar = str.toCharArray();
- String temp;
- List<CharInfo> list = new ArrayList<CharInfo>();
- CharInfo charInfo = null;
- char tempChar ;
- for(int i = 0 ; i < sourceChar.length ; i++){
- tempChar = sourceChar[i];
- temp = String.valueOf(tempChar);
- //构建字符信息(字符,长度)
- charInfo = new CharInfo(temp.getBytes().length, tempChar);
- list.add(charInfo);
- }
- StringBuffer result = new StringBuffer();
- int currentLength = 0 ;
- //遍历字符信息列表,根据目标字节长度返回信息
- for(CharInfo ci : list){
- if(null == ci){
- continue;
- }
- currentLength += ci.getBytesLength();
- if(currentLength > bytesLength){
- return result.toString();
- }
- result.append(ci.getValue());
- }
- return result.toString();
- }
- public class CharInfo {
- private int bytesLength;
- private char value;
- public CharInfo(int bytesLength , char value){
- this.bytesLength = bytesLength;
- this.value = value;
- }
- public int getBytesLength() {
- return bytesLength;
- }
- public char getValue() {
- return value;
- }
- }
0 0
- css截取截取字符串
- 字符串截取
- 字符串截取
- 字符串截取
- 截取字符串
- 截取字符串
- 截取字符串
- 字符串截取
- 字符串截取
- 截取字符串
- 截取字符串
- 截取字符串
- 截取字符串
- 字符串截取
- 字符串截取
- 字符串截取
- 截取字符串
- 截取字符串
- 考研学校
- iOS UIImageView居中
- ansible 好文收集
- 利用Apache commons pool2构建池应用
- UI - NSObject 映射关系图
- 字符串截取
- Class类文件的结构
- HDU 1896 Stones
- java语言程序设计第十版(Introduce to java) 课后习题 chapter6-10
- Eclipse插件svn和TortoiseSvn版本对应关系
- 开发网站合集
- Hive相关资料整理
- java第十二节java中时间处理
- 谈谈Http