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

解决方案 »

  1.   

    参照以下代码,trans是你要的函数 public static void main(String[] args) {
    String mode = "111100111111100011111011";
    System.out.println(trans(mode)); } public static List<String> trans(String mode){
    mode += '0';
    List<String> res = new ArrayList<String>();
    int begin = -1;
    for(int i=0; i<mode.length(); i++){
    if(mode.charAt(i)=='1' && begin<0){
    begin = i;
    }
    if(mode.charAt(i)=='0' && begin>-1){
    res.add(String.format("%02d-%02d", begin, i-1));
    begin = -1;
    }
    }
    return res;
    }
      

  2.   


    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;
    }

    }
      

  3.   


    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);
    }楼主可以改进和封装一下,我写的有点丑。
      

  4.   

    for example
    String mode = "111100111111100011111011";
    Matcher m = Pattern.compile("1+").matcher(mode);//正则查找多个1组成的字符串
    List<String> result = new ArrayList<String>();
    while (m.find()) {//如果能找到正则匹配的字符串
        result.add(String.format("%02d-%02d", m.start(), m.end()-1)); //追加匹配的开始位置和结束位置
    }
    System.out.println(result);