java 字符串截取问题 F8265506000300java java.lang.String split() 把上面 两个为一组截取为字符串 应该怎么写正则表达式 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 split是根据你指定的字符来进行拆分。你这个2个为一组,不要选择这个方法。 [code=Java]public class T { /** * @param args */ public static void main(String[] args) { Pattern p = Pattern.compile("\\w{1,2}"); Matcher m = p.matcher("F82655060003000");//注意, 这里比楼主需求长一个字符. while(m.find()) { System.out.print(m.group() + ", "); } System.out.println(); //上面是使用正则的结果 //下面是使用自己实现的一个简易split. String[] r = split("F82655060003000", 2); int i = 0; for(; i < r.length; i ++) { System.out.print(r[i] + ", "); } } /** * @author selfimpr * @blog http://blog.csdn.net/lgg201 * @mail: [email protected] * @param target 目标字符串 * @param width 要处理的宽度 * @return 处理之后的字符串数组 * 功能: 将指定字符串按照指定宽度分割 */ public static String[] split(String target, int width) { int length = target.length(); int result_length = (length + width - 1) / width; String[] result = new String[result_length]; int i = 0; while(i < result_length - 1) { result[i] = target.substring(i * width, (i + 1) * width); i++; } //打印最后剩余的字符. result[i] = target.substring(i * width); return result; }}[code] public class T { /** * @param args */ public static void main(String[] args) { Pattern p = Pattern.compile("\\w{1,2}"); Matcher m = p.matcher("F82655060003000");//注意, 这里比楼主需求长一个字符. while(m.find()) { System.out.print(m.group() + ", "); } System.out.println(); //上面是使用正则的结果 //下面是使用自己实现的一个简易split. String[] r = split("F82655060003000", 2); int i = 0; for(; i < r.length; i ++) { System.out.print(r[i] + ", "); } } /** * @author selfimpr * @blog http://blog.csdn.net/lgg201 * @mail: [email protected] * @param target 目标字符串 * @param width 要处理的宽度 * @return 处理之后的字符串数组 * 功能: 将指定字符串按照指定宽度分割 */ public static String[] split(String target, int width) { int length = target.length(); int result_length = (length + width - 1) / width; String[] result = new String[result_length]; int i = 0; while(i < result_length - 1) { result[i] = target.substring(i * width, (i + 1) * width); i++; } //打印最后剩余的字符. result[i] = target.substring(i * width); return result; }} String str = "F8265506000300"; for(int i=0;i<str.length();i+=2){ int end = 0; if(i+2>str.length()){//这里是处理字符串str里字符个数的奇偶数,以免substring方法出现 java.lang.StringIndexOutOfBoundsException的错误 end = i+1; }else{ end = i+2; } String sub = str.substring(i, end); System.out.println("sub "+i+" = " + sub); } substring和定义自己的split方法都能实现。5楼的,学习了。 自己定义的split里用到的依然是substring方法,那又何必多此一举呢? String spStr[] = str.split(",");for(int i = 0; i < spStr.length; i++)if( spStr[i].indexOf("CN=") >= 0 )System.out.println(spStr[i]); 或者用charAt(i) ,i%2==0输一次 spring的JPA连接多个数据库 javascript checkbox 问题 关于jar包 有朋友在项目中使用过JDK6自带的JWS吗? 毕业设计----高山上,跪求! 在STRUTS中如何处理查询的到的RESULT SET ? hibernate one to many 问题! 如何找到java.naming.factory.initial(tomcat4.0+javabean) 请教一个java问题 jdom中使用名字空间的问题 我的神啊,09年最后一件事:java整合flex,求支持! java菜鸟问题
public class T { /**
* @param args
*/
public static void main(String[] args) {
Pattern p = Pattern.compile("\\w{1,2}");
Matcher m = p.matcher("F82655060003000");//注意, 这里比楼主需求长一个字符.
while(m.find()) {
System.out.print(m.group() + ", ");
}
System.out.println();
//上面是使用正则的结果
//下面是使用自己实现的一个简易split.
String[] r = split("F82655060003000", 2);
int i = 0;
for(; i < r.length; i ++) {
System.out.print(r[i] + ", ");
}
}
/**
* @author selfimpr
* @blog http://blog.csdn.net/lgg201
* @mail: [email protected]
* @param target 目标字符串
* @param width 要处理的宽度
* @return 处理之后的字符串数组
* 功能: 将指定字符串按照指定宽度分割 */
public static String[] split(String target, int width) {
int length = target.length();
int result_length = (length + width - 1) / width;
String[] result = new String[result_length];
int i = 0;
while(i < result_length - 1) {
result[i] = target.substring(i * width, (i + 1) * width);
i++;
}
//打印最后剩余的字符.
result[i] = target.substring(i * width);
return result;
}}
[code]
public class T { /**
* @param args
*/
public static void main(String[] args) {
Pattern p = Pattern.compile("\\w{1,2}");
Matcher m = p.matcher("F82655060003000");//注意, 这里比楼主需求长一个字符.
while(m.find()) {
System.out.print(m.group() + ", ");
}
System.out.println();
//上面是使用正则的结果
//下面是使用自己实现的一个简易split.
String[] r = split("F82655060003000", 2);
int i = 0;
for(; i < r.length; i ++) {
System.out.print(r[i] + ", ");
}
}
/**
* @author selfimpr
* @blog http://blog.csdn.net/lgg201
* @mail: [email protected]
* @param target 目标字符串
* @param width 要处理的宽度
* @return 处理之后的字符串数组
* 功能: 将指定字符串按照指定宽度分割 */
public static String[] split(String target, int width) {
int length = target.length();
int result_length = (length + width - 1) / width;
String[] result = new String[result_length];
int i = 0;
while(i < result_length - 1) {
result[i] = target.substring(i * width, (i + 1) * width);
i++;
}
//打印最后剩余的字符.
result[i] = target.substring(i * width);
return result;
}}
String str = "F8265506000300";
for(int i=0;i<str.length();i+=2){
int end = 0;
if(i+2>str.length()){//这里是处理字符串str里字符个数的奇偶数,以免substring方法出现 java.lang.StringIndexOutOfBoundsException的错误
end = i+1;
}else{
end = i+2;
}
String sub = str.substring(i, end);
System.out.println("sub "+i+" = " + sub);
}
for(int i = 0; i < spStr.length; i++)
if( spStr[i].indexOf("CN=") >= 0 )
System.out.println(spStr[i]);