字符串转化

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

请大家帮忙写个函数
...全文
182 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
magong 2012-05-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

for example
Java code
String mode = "111100111111100011111011";
Matcher m = Pattern.compile("1+").matcher(mode);//正则查找多个1组成的字符串
List<String> result = new ArrayList<String>();
while (m.find()) {//如果……
[/Quote]
不错,比我的短。收藏了。
zyl2001 2012-05-28
  • 打赏
  • 举报
回复
牛,羡慕嫉妒恨。。。
哈哈。
cstur4 2012-05-28
  • 打赏
  • 举报
回复
果然是大牛[Quote=引用 7 楼 的回复:]

for example
Java code
String mode = "111100111111100011111011";
Matcher m = Pattern.compile("1+").matcher(mode);//正则查找多个1组成的字符串
List<String> result = new ArrayList<String>();
while (m.find()) {//如果……
[/Quote]
qybao 2012-05-28
  • 打赏
  • 举报
回复
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);
zyl2001 2012-05-28
  • 打赏
  • 举报
回复
谢谢各位,结贴给分了。
sdojqy1122 2012-05-28
  • 打赏
  • 举报
回复

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

楼主可以改进和封装一下,我写的有点丑。
cstur4 2012-05-28
  • 打赏
  • 举报
回复

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



}


magong 2012-05-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
String mode = "111100111111100011111011";
代表24个小时,1表示有值,0表示无值
想截取成类似这样的结构“00-03”,“06-12”,"16-20","21-23"...
如果是单个值,则截取成类似“04-04”这样,表示4点有值。

请大家帮忙写个函数
[/Quote]
参照以下代码,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;
}
龙四 2012-05-28
  • 打赏
  • 举报
回复
我表示看不懂
看那边 2012-05-28
  • 打赏
  • 举报
回复
使用正则表达式进行字符串的截取。而且这个截取就比较简单了。

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧