百分求解~一处理字符串的方法. 使用split()函数,把整个字符串分成"x=y"然后再次使用split分解出x和y 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用split()函数,把整个字符串分成"x=y"然后再次使用split分解出x和y String[] temp1 = str.split("&");for(int i=0;i<temp1.length;i++){String[] temp2 = temp1[i].split("=");System.out.println(temp2[0]);System.out.println(temp2[1]);} // 完整的C类实现。public class C { private Map map = null; public C(String str) { map = new HashMap(); splitStr(str); } private void splitStr(String str){ StringTokenizer st = new StringTokenizer(str, "&"); while(st.hasMoreTokens()){ String s = st.nextToken(); int index = s.indexOf("="); String key = s.substring(0, index); String value = s.substring(index+1); map.put(key, value); } } public String getValue(String param){ Object value = map.get(param); if(value != null){ return value.toString(); }else { return null; } }} class C { private String str; public C(String str) { this.str = str; } // -1 代表没有找到 public int getValue(String s) { int rValue = -1; String tmpStr = s + "="; try { if (str != null && !str.equals("")) { int pos1 = str.indexOf(tmpStr); String subStr = str.substring(pos1 + tmpStr.length(), str.length()); int pos2 = subStr.indexOf("&"); pos2 = pos2 == -1 ? subStr.length() : pos2; rValue = Integer.parseInt(subStr.substring(0, pos2)); } return rValue; } catch(NumberFormatException e) { return rValue; } } public static void main(String[] args) { C insC = new C("aa=12&bb=34&cc=56"); System.out.println(insC.getValue("aa")); System.out.println(insC.getValue("cc")); System.out.println(insC.getValue("bb")); System.out.println(insC.getValue("dd")); }} 你的结构用spit()函数和StringTokenizer st = new StringTokenizer(str, "&");都可以来实现。看你自己的处理! 简单写了一个,要求你的JDK要1.4以上import java.util.regex.*;public class TTest { public static void main(String[] args) { String str = "a_=3&b=4&c=5&d=7"; String[] strs=str.split("&"); for(int i=0;i<strs.length;i++){ //通过正则表达式分解字符串 Pattern pattern = Pattern.compile("([\\w]*)=([\\w]*)"); Matcher matcher = pattern.matcher(strs[i]); while (matcher.find()){ String key=matcher.group(1); String value=matcher.group(2); System.out.println(key+":"+value); } } }} 非常感谢Apollo47(阿波罗)与esunboy(天使联盟),两个不同的解决办法都成功的解决了我的问题,也谢谢给我提出建议的各位朋友,以后要自己多多写了,谢谢~~~ 同样谢谢nc201(Bricklayer) 的新的思路。 try:class C{ private String str; public C(String str){ this.str = str; } public int getValue(String s) throws NotInStrException{ int index = str.indexOf(s + "="); if(index < 0) throw new NotInStrException("Out of mind"); int index2 = str.indexOf("&",index); if(index2 < 0) index2 = str.length(); return Integer.parseInt(str.substring(index + s.length() + 1,index2)); }}class NotInStrException extends Exception{ public NotInStrException(){} public NotInStrException(String msg){ super(msg); }} import java.util.HashMap;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author unascribed * @version 1.0 */public class MySplit { private HashMap record = new HashMap(); private MySplit() { } public static MySplit getInstance( String str ){ MySplit ms = new MySplit(); ms.init( str ); return ms; } public String getValue( String key ){ return (String)record.get( key ); } private void init( String s ){ String[] xeqy = s.split("&"); for(int i=0;i<xeqy.length;i++){ String[] xy = xeqy[i].split("="); record.put( xy[ 0 ], xy[ 1 ] ); } } /** * */ public static void main(String[] args) { MySplit ms = MySplit.getInstance( "a=3&b=4&c=5" ); String a = ms.getValue("a"); String b = ms.getValue("b"); String c = ms.getValue("c"); String d = ms.getValue("d"); System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); }} esunboy(天使联盟)的有点漏洞。如aa=1&a=1这样就不对了。修改以下两句:this.str = "&" + str;String tmpStr = "&" + s + "="; 谢谢楼主指出我程序中的bug, 但楼主的写法(String tmpStr = "&" + s + "=";)也有问题, 如果是aa=1&a=1, 如果要getValue("aa")就不能查不出来了。 Apollo47(阿波罗) 的写法不错, 建议楼主采用。其实正则表达式应该是速度应该是最快的。 http://expert.csdn.net/Expert/TopicView1.asp?id=3063668 JAVA怎么用位运算求一个int的绝对值 Tomcat 配置jndi 连接数据库方法 布局怎么也实现不了我想要的..... MQ对列管理器通信问题 求助大侠帮我看看俄罗斯方块的代码,非常感谢 为什么我的电脑装jdk1.5时电脑未响应,跟硬件配置有关系吗?cpu:塞扬333 内存:96M.请大侠指教,谢谢. 怎么把一个string型的串赋给dataInputStream? 写servlet时碰到的疑惑? 彻底拯救我的jdk环境变量设置!! 请问大家,用java作图形应用程序可以脱离浏览器吗,一定需要GUI的图形操作系统支持吗? 请教关于HashMap的问题 关于JAVA的基本函数功能问题。。。
for(int i=0;i<temp1.length;i++){
String[] temp2 = temp1[i].split("=");
System.out.println(temp2[0]);
System.out.println(temp2[1]);
}
public class C {
private Map map = null;
public C(String str) {
map = new HashMap();
splitStr(str);
}
private void splitStr(String str){
StringTokenizer st = new StringTokenizer(str, "&");
while(st.hasMoreTokens()){
String s = st.nextToken();
int index = s.indexOf("=");
String key = s.substring(0, index);
String value = s.substring(index+1);
map.put(key, value);
}
}
public String getValue(String param){
Object value = map.get(param);
if(value != null){
return value.toString();
}else {
return null;
}
}}
private String str; public C(String str) {
this.str = str;
} // -1 代表没有找到
public int getValue(String s) {
int rValue = -1;
String tmpStr = s + "=";
try {
if (str != null && !str.equals("")) {
int pos1 = str.indexOf(tmpStr);
String subStr = str.substring(pos1 + tmpStr.length(), str.length());
int pos2 = subStr.indexOf("&");
pos2 = pos2 == -1 ? subStr.length() : pos2;
rValue = Integer.parseInt(subStr.substring(0, pos2));
}
return rValue;
}
catch(NumberFormatException e) {
return rValue;
}
}
public static void main(String[] args) {
C insC = new C("aa=12&bb=34&cc=56");
System.out.println(insC.getValue("aa"));
System.out.println(insC.getValue("cc"));
System.out.println(insC.getValue("bb"));
System.out.println(insC.getValue("dd"));
}
}
都可以来实现。看你自己的处理!
public class TTest {
public static void main(String[] args) {
String str = "a_=3&b=4&c=5&d=7";
String[] strs=str.split("&");
for(int i=0;i<strs.length;i++){
//通过正则表达式分解字符串
Pattern pattern = Pattern.compile("([\\w]*)=([\\w]*)");
Matcher matcher = pattern.matcher(strs[i]);
while (matcher.find()){
String key=matcher.group(1);
String value=matcher.group(2);
System.out.println(key+":"+value);
}
} }
}
private String str;
public C(String str){
this.str = str;
}
public int getValue(String s) throws NotInStrException{
int index = str.indexOf(s + "=");
if(index < 0) throw new NotInStrException("Out of mind");
int index2 = str.indexOf("&",index);
if(index2 < 0) index2 = str.length();
return Integer.parseInt(str.substring(index + s.length() + 1,index2));
}
}class NotInStrException extends Exception{
public NotInStrException(){}
public NotInStrException(String msg){
super(msg);
}
}
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/public class MySplit { private HashMap record = new HashMap();
private MySplit() {
}
public static MySplit getInstance( String str ){
MySplit ms = new MySplit();
ms.init( str );
return ms;
} public String getValue( String key ){
return (String)record.get( key );
}
private void init( String s ){
String[] xeqy = s.split("&");
for(int i=0;i<xeqy.length;i++){
String[] xy = xeqy[i].split("=");
record.put( xy[ 0 ], xy[ 1 ] );
} }
/**
*
*/
public static void main(String[] args) {
MySplit ms = MySplit.getInstance( "a=3&b=4&c=5" );
String a = ms.getValue("a");
String b = ms.getValue("b");
String c = ms.getValue("c");
String d = ms.getValue("d");
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}
修改以下两句:
this.str = "&" + str;
String tmpStr = "&" + s + "=";
Apollo47(阿波罗) 的写法不错, 建议楼主采用。
其实正则表达式应该是速度应该是最快的。