Java Data Type How to - Split a string containing question marks and equals








Question

We would like to know how to split a string containing question marks and equals.

Answer

import java.util.HashMap;
import java.util.Map;
/* w ww  . ja  v  a 2s .  co m*/
public class Main {
  public static void main(String[] args) {
    String query = "name==p==?header=hello?aname=?????lname=lastname";
    String[] params = query.split("\\?");
    Map<String, String> map = new HashMap<String, String>();
    for (String param : params) {
      String name = param.split("=")[0];
      String value = param.substring(name.length(), param.length());
      map.put(name, value);
      System.out.println(name);
      if (name.equals("")) {
        value += "?";
      }
      System.out.println(value.replaceAll(" ", ""));
    }
  }
}