关于字符串的截取 1|202|27|0730|07302088169|07302088169|20070705 121212||20100801 000000|201000831 235959||||||4|0|500||2|||||||||现需要将|前面的数据截取出来,如果是空则不要。求高手帮忙 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先搞个split函数,在用这个方法得到String数组 /** * * 自定义字符串分割函数,返回数组 -- by PanJun * * @param s * 待分解串 * @param token * 分解标识 * @return * */ public static String[] split(String s, String token) { if (s == null) return null; if (token == null || s.length() == 0) return new String[] { s }; int size = 0; String[] result = new String[4]; while (s.length() > 0) { int index = s.indexOf(token); String splitOne = s; if (index > -1) { splitOne = s.substring(0, index); s = s.substring(index + token.length()); } else { s = ""; } if (size >= result.length) { String[] tmp = new String[result.length * 2]; System.arraycopy(result, 0, tmp, 0, result.length); result = tmp; } if (splitOne.length() > 0) { result[size++] = splitOne; } } String[] tmp = result; result = new String[size]; System.arraycopy(tmp, 0, result, 0, size); return result; } public static void main(String[] args){ String str="1|202|27|0730|07302088169|07302088169|20070705 121212||20100801 000000|201000831 235959||||||4|0|500||2|||||||||"; String[] arr=str.split("(?<!^)\\|+"); for(String s:arr) System.out.println(s); } String str="1|202|27|0730|07302088169|07302088169|20070705 121212||20100801 000000|201000831 235959||||||4|0|500||2|||||||||"; String[] arr=str.split("\\|+"); for(String s:arr) System.out.println(s);不用加(?<!^)也可以。要是字符创以|开头的就要加(?<!^) 使用split函数 一般的这种题目都是这么写的 The method getMsg() from the type test1 is not visible 请教java程序如何发送一个enq啊? org.apache和org.eclipse包是什么包,如何使用 请教一下,如何通过java串口编程来读写IC\ID卡的数据呀 培训管用吗? 网络高手请进 怎么设置环境变量,使得能调用WEB SERVET类 用java实现数据库编程,大家给点意见 关于JNI,保证给分,多谢,急用 Java的SQL语句执行错误问题 帮看一段代码啊,视屏里的一段 一道SCJP的题目
*
* 自定义字符串分割函数,返回数组 -- by PanJun
*
* @param s
* 待分解串
* @param token
* 分解标识
* @return
*
*/
public static String[] split(String s, String token) {
if (s == null)
return null; if (token == null || s.length() == 0)
return new String[] { s }; int size = 0;
String[] result = new String[4];
while (s.length() > 0) {
int index = s.indexOf(token);
String splitOne = s;
if (index > -1) {
splitOne = s.substring(0, index);
s = s.substring(index + token.length());
} else {
s = "";
} if (size >= result.length) {
String[] tmp = new String[result.length * 2];
System.arraycopy(result, 0, tmp, 0, result.length);
result = tmp;
} if (splitOne.length() > 0) {
result[size++] = splitOne;
}
} String[] tmp = result;
result = new String[size];
System.arraycopy(tmp, 0, result, 0, size);
return result;
}
public static void main(String[] args){
String str="1|202|27|0730|07302088169|07302088169|20070705 121212||20100801 000000|201000831 235959||||||4|0|500||2|||||||||";
String[] arr=str.split("(?<!^)\\|+");
for(String s:arr)
System.out.println(s); }
String str="1|202|27|0730|07302088169|07302088169|20070705 121212||20100801 000000|201000831 235959||||||4|0|500||2|||||||||";
String[] arr=str.split("\\|+");
for(String s:arr)
System.out.println(s);不用加(?<!^)也可以。要是字符创以|开头的就要加(?<!^)
使用split函数 一般的这种题目都是这么写的