首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

字符串转化,该如何解决

2012-05-29 
字符串转化String mode 111100111111100011111011代表24个小时,1表示有值,0表示无值想截取成类似这样

字符串转化
String mode = "111100111111100011111011";
代表24个小时,1表示有值,0表示无值
想截取成类似这样的结构“00-03”,“06-12”,"16-20","21-23"...
如果是单个值,则截取成类似“04-04”这样,表示4点有值。

请大家帮忙写个函数

[解决办法]
使用正则表达式进行字符串的截取。而且这个截取就比较简单了。
[解决办法]

探讨
String mode = "111100111111100011111011";
代表24个小时,1表示有值,0表示无值
想截取成类似这样的结构“00-03”,“06-12”,"16-20","21-23"...
如果是单个值,则截取成类似“04-04”这样,表示4点有值。

请大家帮忙写个函数

[解决办法]
Java code
import java.util.ArrayList;import java.util.List;/** *  * @author cstur4 * */public class CSDN {    public static void main(String[] args) {                String mode = "111100111111100011111011";        List<String> res = conversionMode(mode);        for(String s:res)            System.out.println(s);            }    public static List<String> conversionMode(String mode){        List<String> res = new ArrayList<String>();        int index = 0;        while(index<mode.length()){            if(mode.charAt(index)=='1'){                int end = getRightIndex(mode, index);                String newMode = fillWith0(index)+"-"+fillWith0(end);                res.add(newMode);                index = end+1;            }else                index++;        }        return res;    }    private static int getRightIndex(String mode, int index) {                while(index<mode.length() && mode.charAt(index)=='1')            ++index;        return index-1;    }        private static String fillWith0(int num){                String value = String.valueOf(num);        if(value.length()==1)            return "0"+num;        return value;    }        }
[解决办法]
Java code
public static void main(String args[]) {        List<String> list = new ArrayList<String>();        String mode = "111100111111100011111011";        char[] data = mode.toCharArray();        int startIndex = -1;        int endIndex = -1;        for (int i = 0; i < data.length; i++) {            if (data[i] == '1') {                if (startIndex == -1) {                    if(i == data.length - 1){                        String tempStart = ("00"+i).replaceAll(".*?(\\d{2})$", "$1");                        String tempEnd = ("00"+i).replaceAll(".*?(\\d{2})$", "$1");                        list.add(tempStart + "-" + tempEnd);                        startIndex = -1;                        endIndex = -1;                    }                    else{                        startIndex = i;                        endIndex = i;                    }                } else {                    if(i == data.length - 1){                        String tempStart = ("00"+startIndex).replaceAll(".*?(\\d{2})$", "$1");                        String tempEnd = ("00"+i).replaceAll(".*?(\\d{2})$", "$1");                        list.add(tempStart + "-" + tempEnd);                        startIndex = -1;                        endIndex = -1;                    }                    else{                        endIndex = i;                    }                                    }            }            if (data[i] == '0') {                if (startIndex != -1) {                    String tempStart = ("00"+startIndex).replaceAll(".*?(\\d{2})$", "$1");                    String tempEnd = ("00"+endIndex).replaceAll(".*?(\\d{2})$", "$1");                    list.add(tempStart + "-" + tempEnd);                    startIndex = -1;                    endIndex = -1;                }            }        }        System.out.println(list);    } 

热点排行