Java: 按分隔符将字符串拆分为哈希表

Java: split string into hashtable by delimiters

运行 java 在我的 cgi-bin 中。通过 post 请求。在我的 java 程序中,我将浏览器输出为一个字符串,例如:name=josh&age=34...等等

在我的 java 程序中 String x = "name=joshua";

如何用 = 分隔符将这个 x 字符串拆分成哈希表。

我的哈希表是 Hashtable<String, String>

您可以这样尝试:

// initializes a hashtable for key and value types to be String
Hashtable<String, String> h = 
              new Hashtable<String, String>();
// your string
String x = "name=josh";
// splits the string for "=" delimiter and stores in an array
String[] arrOfStr = x.split("=", 0); 
// Use the 'put' method of Hashtable to insert the 0th element of array as key and 1st element as value
h.put(arrOfStr[0],arrOfStr[1]);