How to do PHP explode(", "), list(), foreach() in Java style
I have PHP code and i am translating it to Java, but how do we do PHP like explode(), list(), foreach() with Java? Following PHP code are my example problems:
1. PHP:
/**
* @return: Array
*/
public function parse($digest)
{
$array = array();
$parts = explode(", ", $digest);
开发者_StackOverflow社区 foreach ($parts as $x)
{
$bits = explode("=", $x);
$data[$bits[0]] = str_replace('"','', $bits[1]);
}
return $array;
}
Java (updated):
private String[] parse(String digest)
{
Map<String,String> array = new HashMap<String, String>();
String[] parts = digest.split(", ");
for (String x:parts)
{
String[] bits = x.split("=");
array.put(bits[0], bits[1].replace( '"','' )); // later will apply regex
}
return array; // incompatible types
}
2. PHP:
/**
* @return: String
*/
public function response($text, $user, $pass, $httpmethod, $uri)
{
list($dummy_digest, $value) = split(' ', $text, 2);
$x = $this->parse($value);
$realm = $x['realm'];
if ($x['qop'] == 'auth')
{
...
}
$base = "{$realm}, {$x['realm']}";
//"Digest username="1", realm="1", nonce="xxxxxx", uri="abc", response="abc", qop="auth", algorithm="MD5", cnonce="", nc="1"";
return $base;
}
Java problem:
public String response(String text, String user, String pass, String httpmethod, String uri)
{
// how to do such list() like php?
return "Digest username="1", realm="1", nonce="xxxxxx", uri="abc", response="abc", qop="auth", algorithm="MD5", cnonce="", nc="1"";
}
it's pretty much the same going to java, you could parse the data with:
Map<String, String> key_map = new HashMap<String, String>();
String[] parts_of_string = "var=test, var2=test2".split(", ");
for(String part : parts_of_string) {
String[] key_and_value_parts = part.split("=");
key_map.put(key_and_value_parts[0], key_and_value_parts[1]);
System.out.println(key_and_value_parts[0] + " <-> " + key_and_value_parts[1]);
}
Update (Second Method):
Java does not handle arrays quite the same as PHP. Where php can refrence a object in array by using a string like $array["my_var"] , Java can only reference by their index in the array. So you will need to use a map, this allows you to map a certain key to a value. You could parse the text like so:
private static Map<String, String> parse(String digest) {
String[] parts = digest.split(", ");
Map<String, String> map = new HashMap<String, String>();
for (String x : parts) {
String[] bits = x.split("=");
//Remove "'s here if you want to keep them in the string
map.put(bits[0], bits[1]);
}
return map;
}
You can then access any information given by this map by using the method get on it. You will then specify the key you want and the map will return the value associated with that key. Be sure to use .containsKey to check if the Ma contains the key you are looking for.
So when you have parsed your information with the previous function you can then access the values by using this for example:
Map<String, String> parsed_map = parse("A realm=\"1234\", B=\"3434df323423423\", C=\"300.00\", D=\"loopback\"'");
String info = parsed_map.get("B");
System.out.println(info);
Hope that helps, else ask away :D
PS: I haven't actually tested this just be sure to check that it runs as expected.
Explode - StringUtils.splitPreserveAllTokens(String str, char separatorChar)
foreach is just written differently - for(String s : array)
I don't think there is any equivalent in Java for list, I don't think it'd be possible to write one either as all variables in Java are "pass by value".
example foreach()
in Java inside of (just as sample) clone()
method (with deep cloning - prototype design-pattern)
public class Order {
private List<Item> items = new ArrayList<Item>();
public Order clone() {
Order newOrder = new Order();
for (Item originalItem : this.items) {
Item newItem = new Item();
newItem.setPrice(originalItem.getPrice());
newOrder.items.add(newItem);
}
return newOrder;
}
it should be realy clear now, how to use foreach, hope it helps
精彩评论