why is "+" added to all my coordinates in url builder? - java
I have this code to build a string of coordinates:
public String generateUrl(CompleteRoutingResponseShort completeRoutingResponse) {
if (completeRoutingResponse == null)
{
return "CompleteRoutingResponse is null";
}
UriBuilder builder = UriBuilder
.fromPath(Constants.LIVEMAP_BASE_URL)
.scheme("http");
builder.queryParam("lineGeom", getCoordsLists(completeRoutingResponse));
return builder.build().toString();
}
private String getCoordsLists() {
List<String> lists = new ArrayList<>();
for (AlternativeShort alternative : completeRoutingResponses.alternatives)
{
String list = "( " + alternative.coords.stream().map(item -> String.format("%.4f , %.4f", item.x, item.y))
.collect(Collectors.joining(", ")) + ")";
// list.replace("+","");
lists.add(list);
}
return String.join(", ", lists);
}
however I get this url as an output:
"http:/myhost.com/tiles/internal?lineGeom=(+34.9178+,+31.9550,+34.9175+,+31.9552,+34.9174+,+31.9553,+34.9172+,+31.9555,+34.9171+,+31.9556,+34.9171+,+31.9556,+34.9168+,+31.9559,+34.9168+,+31.9559,+34.9167+,+31.9561,+34.9165+,+31.9562,+34.9164+,+31.9564,+34.9161+,+31.9567,+34.9161+,+31.9568,+34.9160+,+31.9570,+34.9159+,+31.9571,+34.9158+,+31.9572,+34.9158+,+31.9573,+34.9157+,+31.9574,+34.9157+,+31.9574,+34.9166+,+31.9577,+34.9166+,+31.9577,+34.9178+,+31.9581,+34.9184+,+31.9583,+34.9188+,+31.9583,+34.9192+,+31.9584,+34.9195+,+31.9585,+34.9201+,+31.9585,+34.9204+,+31.9585,+34.9208+,+31.9585,+34.9211+,+31.9585,+34.9214+,+31.9585,+34.9216+,+31.9585,+34.9222+,+31.9584,+34.9224+,+31.9584,+34.9227+,+31.9583,+34.9233+,+31.9582,+34.9233+,+31.9582,+34.9238+,+31.9580,+34.9246+,+31.9577,+34.9257+,+31.9572,+34.9262+,+31.9571,+34.9265+,+31.9569,+34.9266+,+31.9569,+34.9268+,+31.9569,+34.9271+,+31.9568,+34.9276+,+31.9567,+34.9281+,+31.9566,+34.9284+,+31.9566,+34.9289+,+31.9566,+34.9307+,+31.9565,+34.9314+,+31.9564,+34.9320+,+31.9564,+34.9325+,+31.9563,+34.9325+,+31.9563,+34.9330+,+31.9563,+34.9335+,+31.9563,+34.9340+,+31.9562,+34.9345+,+31.9562,+34.9345+,+31.9562,+34.9357+,+31.9560,+34.9361+,+31.9560,+34.9365+,+31.9559,+34.9365+,+31.9559,+34.9369+,+31.9559,+34.9373+,+31.9558,+34.9373+,+31.9558,+34.9376+,+31.9557,+34.9377+,+31.9556,+34.9382+,+31.9554,+34.9387+,+31.9549,+34.9387+,+31.9549,+34.9389+,+31.9547,+34.9394+,+31.9542,+34.9394+,+31.9542,+34.9395+,+31.9543,+34.9395+,+31.9543,+34.9398+,+31.9545,+34.9399+,+31.9546,+34.9399+,+31.9546,+34.9401+,+31.9548,+34.9407+,+31.9555,+34.9414+,+31.9565,+34.9417+,+31.9571,+34.9417+,+31.9571,+34.9418+,+31.9572,+34.9418+,+31.9572,+34.9420+,+31.9578,+34.9420+,+31.9579,+34.9421+,+31.9585,+34.9421+,+31.9585,+34.9421+,+31.9590,+34.9421+,+31.9599,+34.9421+,+31.9599,+34.9422+,+31.9607,+34.9422+,+31.9608,+34.9423+,+31.9609,+34.9423+,+31.9610,+34.9423+,+31.9610,+34.9423+,+31.9611,+34.9424+,+31.9611,+34.9425+,+31.9612,+34.9426+,+31.9613,+34.9426+,+31.9613,+34.9426+,+31.9613,+34.9429+,+31.9616,+34.9432+,+31.9618,+34.9436+,+31.9621,+34.9449+,+31.9630,+34.9449+,+31.9630,+34.9487+,+31.9649,+34.9487+,+31.9649,+34.9525+,+31.9668,+34.9525+,+31.9668,+34.9531+,+31.9671,+34.9535+,+31.9672,+34.9538+,+31.9674,+34.9542+,+31.9676,+34.9546+,+31.9678,+34.9550+,+31.9680,+34.9553+,+31.9682,+34.9554+,+31.9682,+34.9558+,+31.9684,+34.9566+,+31.9689,+34.9570+,+31.9691,+34.9573+,+31.9694,+34.9579+,+31.9698,+34.9582+,+31.9701,+34.9586+,+31.9704,+34.9591+,+31.9709,+34.9594+,+31.9713,+34.9599+,+31.9720,+34.9602+,+31.9725,+34.9605+,+31.9730,+34.9608+,+31.9736,+34.9610+,+31.9742,+34.9612+,+31.9746,+34.9613+,+31.9752,+34.9614+,+31.9756,+34.9615+,+31.9764,+34.9627+,+31.9857,+34.9630+,+31.9883,+34.9632+,+31.9892,+34.9634+,+31.9902,+34.9637+,+31.9912,+34.9654+,+31.9961,+34.9658+,+31.9974,+34.9659+,+31.9979,+34.9660+,+31.9984,+34.9662+,+31.9994,+34.9663+,+32.0002,+34.9664+,+32.0011,+34.9671+,+32.0154,+34.9671+,+32.0166,+34.9671+,+32.0169,+34.9671+,+32.0176,+34.9670+,+32.0179,+34.9669+,+32.0185,+34.9668+,+32.0192,+34.9666+,+32.0198,+34.9663+,+32.0206,+34.9660+,+32.0211,+34.9656+,+32.0218,+34.9652+,+32.0225,+34.9649+,+32.0229,+34.9644+,+32.0236,+34.9627+,+32.0256,+34.9607+,+32.0279,+34.9602+,+32.0285,+34.9596+,+32.0292,+34.9587+,+32.0300,+34.9534+,+32.0351,+34.9530+,+32.0356,+34.9525+,+32.0361,+34.9520+,+32.0368,+34.9515+,+32.0374,+34.9414+,+32.0522,+34.9408+,+32.0531,+34.9404+,+32.0539,+34.9399+,+32.0548,+34.9396+,+32.0555,+34.9392+,+32.0567,+34.9381+,+32.0603,+34.9381+,+32.0603,+34.9357+,+32.0679,+34.9357+,+32.0679,+34.9341+,+32.0730,+34.9341+,+32.0730,+34.9336+,+32.0747,+34.9331+,+32.0765,+34.9331+,+32.0765,+34.9330+,+32.0767,+34.9330+,+32.0768,+34.9328+,+32.0781,+34.9328+,+32.0781,+34.9328+,+32.0793,+34.9328+,+32.0793,+34.9333+,+32.0832,+34.9341+,+32.0899,+34.9351+,+32.0989,+34.9358+,+32.1057,+34.9359+,+32.1064,+34.9359+,+32.1064,+34.9359+,+32.1075,+34.9361+,+32.1093,+34.9363+,+32.1106,+34.9366+,+32.1119,+34.9369+,+32.1130,+34.9369+,+32.1131,+34.9373+,+32.1141,+34.9375+,+32.1146,+34.9381+,+32.1159,+34.9387+,+32.1171,+34.9391+,+32.1178,+34.9391+,+32.1178,+34.9399+,+32.1189,+34.9403+,+32.1195,+34.9410+,+32.1204,+34.9411+,+32.1204,+34.9414+,+32.1208,+34.9442+,+32.1238,+34.9477+,+32.1273,+34.9477+,+32.1273,+34.9497+,+32.1294,+34.9497+,+32.1294,+34.9533+,+32.1331,+34.9533+,+32.1331,+34.9554+,+32.1350,+34.9556+,+32.1351,+34.9558+,+32.1352,+34.9562+,+32.1354,+34.9564+,+32.1355,+34.9566+,+32.1355,+34.9578+,+32.1358,+34.9582+,+32.1360,+34.9586+,+32.1361,+34.9589+,+32.1363,+34.9591+,+32.1364,+34.9593+,+32.1366,+34.9595+,+32.1368,+34.9596+,+32.1370,+34.9598+,+32.1372,+34.9598+,+32.1374,+34.9599+,+32.1377,+34.9600+,+32.1379,+34.9600+,+32.1381,+34.9600+,+32.1383,+34.9599+,+32.1385,+34.9599+,+32.1387,+34.9598+,+32.1389,+34.9597+,+32.1392,+34.9596+,+32.1393,+34.9595+,+32.1395,+34.9594+,+32.1397,+34.9592+,+32.1399,+34.9591+,+32.1400,+34.9589+,+32.1402,+34.9587+,+32.1403,+34.9560+,+32.1421,+34.9560+,+32.1421,+34.9515+,+32.1449,+34.9515+,+32.1449,+34.9513+,+32.1451,+34.9513+,+32.1451,+34.9509+,+32.1453,+34.9509+,+32.1453,+34.9504+,+32.1457,+34.9504+,+32.1457,+34.9456+,+32.1488,+34.9439+,+32.1500,+34.9431+,+32.1505,+34.9431+,+32.1505,+34.9423+,+32.1512,+34.9403+,+32.1527,+34.9399+,+32.1531,+34.9371+,+32.1556,+34.9324+,+32.1598,+34.9297+,+32.1622,+34.9297+,+32.1622,+34.9280+,+32.1635,+34.9265+,+32.1645,+34.9254+,+32.1650,+34.9237+,+32.1658,+34.9237+,+32.1658,+34.9237+,+32.1658,+34.9235+,+32.1659,+34.9223+,+32.1663,+34.9213+,+32.1665,+34.9209+,+32.1666,+34.9207+,+32.1667,+34.9196+,+32.1670,+34.9167+,+32.1676,+34.9167+,+32.1676,+34.9146+,+32.1681,+34.9124+,+32.1686,+34.9121+,+32.1686,+34.9112+,+32.1688,+34.9101+,+32.1689,+34.9089+,+32.1690,+34.9065+,+32.1693,+34.9056+,+32.1694,+34.9045+,+32.1695,+34.9038+,+32.1697,+34.9032+,+32.1698,+34.9031+,+32.1698,+34.9025+,+32.1700,+34.9022+,+32.1701,+34.9018+,+32.1702,+34.9015+,+32.1703,+34.9002+,+32.1708,+34.8985+,+32.1714,+34.8985+,+32.1714,+34.8982+,+32.1714,+34.8981+,+32.1714,+34.8977+,+32.1714,+34.8975+,+32.1714,+34.8972+,+32.1714,+34.8970+,+32.1714,+34.8963+,+32.1716,+34.8938+,+32.1721,+34.8935+,+32.1722,+34.8935+,+32.1722,+34.8935+,+32.1723,+34.8935+,+32.1723,+34.8935+,+32.1727,+34.8935+,+32.1727,+34.8935+,+32.1728,+34.8935+,+32.1729,+34.8935+,+32.1729,+34.8935+,+32.1730,+34.8934+,+32.1732,+34.8933+,+32.1734,+34.8933+,+32.1734,+34.8922+,+32.1744,+34.8922+,+32.1744,+34.8913+,+32.1753,+34.8913+,+32.1753,+34.8906+,+32.1761,+34.8906+,+32.1761,+34.8903+,+32.1763,+34.8899+,+32.1769,+34.8896+,+32.1772,+34.8896+,+32.1772,+34.8891+,+32.1775,+34.8887+,+32.1778,+34.8886+,+32.1781,+34.8886+,+32.1781,+34.8891+,+32.1804,+34.8891+,+32.1804,+34.8893+,+32.1812,+34.8893+,+32.1812,+34.8900+,+32.1846,+34.8900+,+32.1846,+34.8902+,+32.1853,+34.8902+,+32.1856,+34.8902+,+32.1857,+34.8898+,+32.1886,+34.8898+,+32.1886,+34.8896+,+32.1896,+34.8896+,+32.1896,+34.8893+,+32.1912,+34.8893+,+32.1914,+34.8892+,+32.1916,+34.8892+,+32.1917,+34.8893+,+32.1919,+34.8893+,+32.1920,+34.8894+,+32.1920,+34.8895+,+32.1921,+34.8896+,+32.1922,+34.8897+,+32.1922,+34.8899+,+32.1922,+34.8900+,+32.1922,+34.8902+,+32.1922,+34.8903+,+32.1921,+34.8903+,+32.1920,+34.8904+,+32.1919,+34.8904+,+32.1918,+34.8905+,+32.1917,+34.8905+,+32.1916,+34.8904+,+32.1912,+34.8904+,+32.1912,+34.8898+,+32.1911,+34.8898+,+32.1911,+34.8891+,+32.1911,+34.8891+,+32.1911,+34.8885+,+32.1910,+34.8872+,+32.1912,+34.8872+,+32.1912,+34.8858+,+32.1913,+34.8858+,+32.1913,+34.8848+,+32.1914,+34.8844+,+32.1915,+34.8844+,+32.1915,+34.8838+,+32.1916,+34.8838+,+32.1916,+34.8820+,+32.1920,+34.8820+,+32.1920,+34.8811+,+32.1922,+34.8811+,+32.1922,+34.8810+,+32.1925,+34.8810+,+32.1928,+34.8810+,+32.1928,+34.8808+,+32.1934,+34.8808+,+32.1934,+34.8807+,+32.1940,+34.8807+,+32.1940,+34.8805+,+32.1953,+34.8805+,+32.1953,+34.8805+,+32.1958)",
how can I remove the + signs ?
In your getCoordsLists() method you are joining the collectors with ", "
thats why, after building the url it is adding spaces after every coordinate.
so just replace the ", " with "," in the following line:
String list = "( " + alternative.coords.stream().map(item -> String.format("%.4f , %.4f", item.x, item.y))
.collect(Collectors.joining(", ")) + ")";
It is happening because default url encoder is replacing the spaces by "+" so that the link will not be broken.
You should also replace the last line in the same way, where you are returning the string.
Related
Array type found error when try to call from an array of string urls
I just have a question in running a Get request based on string urls. The code below determines the string urls and puts each urls in an array for a table row. final String URL_CORE = “/test/platform/auth”; List<String> urls = new ArrayList<>(); rows.forEach(row -> { final String clientValue = row.getCell("client"); final String uriValue = row.getCell("uri"); final String typeValue = row.getCell("type")); urls.add(URL_CORE + "?" + (clientValue.isEmpty ? "" : CLIENT + "=" + clientValue + "&") + (uriValue.isEmpty ? "" : URI + "=" + uriValue + "&") + (typeValue.isEmpty ? "" : TYPE + "=" + typeValue); }); Within the forEach, I have this code where I just want to run the get request for the url string (for each row at a time). It is giving me an erray type expected found and wondering what am I doing wrong? getRequest(queryParamsList[row]);
It seems by the name that queryParamsList is a List, not an array, so you have to access element by list.get(index) method. If you dont have the index, you can get it by list.indexOf(element)
Convert HOCON string into Java object
One of my webservice return below Java string: [ { id=5d93532e77490b00013d8862, app=null, manufacturer=pearsonEducation, bookUid=bookIsbn, model=2019, firmware=[1.0], bookName=devotional, accountLinking=mandatory } ] I have the equivalent Java object for the above string. I would like to typecast or convert the above java string into Java Object. I couldn't type-cast it since it's a String, not an object. So, I was trying to convert the Java string to JSON string then I can write that string into Java object but no luck getting invalid character "=" exception. Can you change the web service to return JSON? That's not possible. They are not changing their contracts. It would be super easy if they returned JSON.
The format your web-service returns has it's own name HOCON. (You can read more about it here) You do not need your custom parser. Do not try to reinvent the wheel. Use an existing one instead. Add this maven dependency to your project: <dependency> <groupId>com.typesafe</groupId> <artifactId>config</artifactId> <version>1.3.0</version> </dependency> Then parse the response as follows: Config config = ConfigFactory.parseString(text); String id = config.getString("id"); Long model = config.getLong("model"); There is also an option to parse the whole string into a POJO: MyResponsePojo response = ConfigBeanFactory.create(config, MyResponsePojo.class); Unfortunately this parser does not allow null values. So you'll need to handle exceptions of type com.typesafe.config.ConfigException.Null. Another option is to convert the HOCON string into JSON: String hoconString = "..."; String jsonString = ConfigFactory.parseString(hoconString) .root() .render(ConfigRenderOptions.concise()); Then you can use any JSON-to-POJO mapper.
Well, this is definitely not the best answer to be given here, but it is possible, at least… Manipulate the String in small steps like this in order to get a Map<String, String> which can be processed. See this example, it's very basic: public static void main(String[] args) { String data = "[\r\n" + " {\r\n" + " id=5d93532e77490b00013d8862, \r\n" + " app=null,\r\n" + " manufacturer=pearsonEducation, \r\n" + " bookUid=bookIsbn, \r\n" + " model=2019,\r\n" + " firmware=[1.0], \r\n" + " bookName=devotional, \r\n" + " accountLinking=mandatory\r\n" + " }\r\n" + "]"; // manipulate the String in order to have String[] splitData = data // no leading and trailing [ ] - cut the first and last char .substring(1, data.length() - 1) // no linebreaks .replace("\n", "") // no windows linebreaks .replace("\r", "") // no opening curly brackets .replace("{", "") // and no closing curly brackets. .replace("}", "") // Then split it by comma .split(","); // create a map to store the keys and values Map<String, String> dataMap = new HashMap<>(); // iterate the key-value pairs connected with '=' for (String s : splitData) { // split them by the equality symbol String[] keyVal = s.trim().split("="); // then take the key String key = keyVal[0]; // and the value String val = keyVal[1]; // and store them in the map ——> could be done directly, of course dataMap.put(key, val); } // print the map content dataMap.forEach((key, value) -> System.out.println(key + " ——> " + value)); } Please note that I just copied your example String which may have caused the line breaks and I think it is not smart to just replace() all square brackets because the value firmware seems to include those as content.
In my opinion, we split the parse process in two step. Format the output data to JSON. Parse text by JSON utils. In this demo code, i choose regex as format method, and fastjson as JSON tool. you can choose jackson or gson. Furthermore, I remove the [ ], you can put it back, then parse it into array. import com.alibaba.fastjson.JSON; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SerializedObject { private String id; private String app; static Pattern compile = Pattern.compile("([a-zA-Z0-9.]+)"); public static void main(String[] args) { String str = " {\n" + " id=5d93532e77490b00013d8862, \n" + " app=null,\n" + " manufacturer=pearsonEducation, \n" + " bookUid=bookIsbn, \n" + " model=2019,\n" + " firmware=[1.0], \n" + " bookName=devotional, \n" + " accountLinking=mandatory\n" + " }\n"; String s1 = str.replaceAll("=", ":"); StringBuffer sb = new StringBuffer(); Matcher matcher = compile.matcher(s1); while (matcher.find()) { matcher.appendReplacement(sb, "\"" + matcher.group(1) + "\""); } matcher.appendTail(sb); System.out.println(sb.toString()); SerializedObject serializedObject = JSON.parseObject(sb.toString(), SerializedObject.class); System.out.println(serializedObject); } }
How to remove specific line from multi-line string
I have below java string as command output String output = "NIC Value\n" + "------ -----\n" + "vmn0 on \n" + "vmn1 on \n" + "vmn2 on \n" + "vmn3 on \n" + "vmn4 on"; I want to remove second line with dash from above string. How can I do it? I tried it using contains method but it is generating blank line after removing second line. if(output!=null && output.contains("-")) output = output.replace("-","");
This is complete answer you are looking for: String output = "NIC Value\n" + "------ -----\n" + "vmn0 on \n" + "vmn1 on \n" + "vmn2 on \n" + "vmn3 on \n" + "vmn4 on"; String str = Stream.of(output.split("\n")) .filter(s -> !s.contains("--")) .collect(Collectors.joining("\n"));
You can use this to remove that line and use the result, String result = output.replace("------ -----\n", ""); It will replace that line with an empty String
accessing list elements in java
I created a linkedlist object as follows importBuffer = new BufferedReader(new FileReader(importcsvFile)); while ((line = importBuffer.readLine()) != null) { // use comma as separator String[] importedFile = line.split(cvsSplitBy); //cap,comune,provincia,stato System.out.println("Codice Azienda " + importedFile[0] + " , Codice Cliente=" + importedFile[1] + " , Regione Sociale=" + importedFile[2] + " , Indrizzo=" + importedFile[3] + " , comune=" + importedFile[4] + " , provincia=" + importedFile[5] + " , stato=" + importedFile[6] +"]"); counter++; PublicDefinition.importList.add(importBuffer.toString()); List customers = select.select(importedFile[0],importedFile[1], importedFile[3]); if(!customers.isEmpty()) { System.out.println("selected Customer : " + customers.size()); buffureList = customers; Object a=List.class.cast(customers); PublicDefinition.testingList.add(buffureList.toString()); System.out.println("selected Customer : " + PublicDefinition.importList.get(0)); System.out.println("selected Customer : " + PublicDefinition.testingList.getFirst()); updateCustomer = customers; if(customers.get(0)==importedFile[0]) System.out.println("Matched Codice Azienda"); select.updateTable(importedFile[1], importedFile[3], "10.34", "11.40"); //String CodiceCliente, String indrizzo, String latitude, String longitute } } when I try to access the elements for the linkedlist using System.out.println("selected Customer : " + PublicDefinition.importList.get(0)); I got the output: selected Customer : java.io.BufferedReader#420dc55b I think this is the memory reference, but I want to retrieve the value of the linkedlist my select function is: public List<Customer> select(String codiceAzienda, String codiceCliente, String indrizzo) { return jdbcTemplate.query( "SELECT * FROM customers WHERE CodiceAzienda= ?", new Object[] { codiceAzienda}, (rs, rowNum) -> new Customer(rs.getLong("id"), rs.getString("CodiceAzienda"), rs.getString("Indrizzo"), rs.getString("codice_cliente"), rs.getString("Indrizzo"))); }
You added the toString() value of the importBuffer object, not the actual contents. The default toString() implementation (which every object inherits from... Object) returns ClassName#HashCode. So your output isn't wrong, but your input is. See Object.toString() in the javadoc
Go ahead and perform: PublicDefinition.importList.add(importBuffer.readLine()); Instead of : PublicDefinition.importList.add(importBuffer.toString()); Since you are trying to output the contents of the buffered reader instead of the ClassName#Hashcode contents.
Replace PublicDefinition.importList.add(importBuffer.toString()); with PublicDefinition.importList.add(importedFile); You are accidentally adding the string representation of BufferReader object, not the list of import files which sounds like your intention.
Best way to split a string containing question marks and equals
Having an issue where I have a java string: String aString="name==p==?header=hello?aname=?????lname=lastname"; I need to split on question marks followed by equals. The result should be key/value pairs: name = "=p==" header = "hello" aname = "????" lname = "lastname" The problem is aname and lname become: name = "" lname = "????lname=lastname" My code simply splits by doing aString.split("\\?",2) which will return 2 strings.One contains a key/value pair and the second string contains the rest of the string. If I find a question mark in the string, I recurse on the second string to further break it down. private String split(String aString) { System.out.println("Split: " + aString); String[] vals = aString.split("\\?",2); System.out.println(" - Found: " + vals.length); for ( int c = 0;c<vals.length;c++ ) { System.out.println(" - "+ c + "| String: [" + vals[c] + "]" ); if(vals[c].indexOf("?") > 0 ) { split(vals[c]); } } return ""; // For now return nothing... } Any ideas how I could allow a name of ? Disclaimer: Yes , My Regex skills are very low, so I don't know if this could be done via a regex expression.
You can let regex do all the heavy lifting, first splitting your string up into pairs: String[] pairs = aString.split("\\?(?!\\?)"); That regex means "a ? not followed by a ?", which gives: [name==p==, header=hello, aname=????, lname=lastname] To then also split the results into name/value, split only the first "=": String[] split = pair.split("=", 2); // max 2 parts Putting it all together: String aString = "name==p==?header=hello?aname=?????lname=lastname"; for (String pair : aString.split("\\?(?!\\?)")) { String[] split = pair.split("=", 2); System.out.println(split[0] + " is " + split[1]); } Output: name is =p== header is hello aname is ???? lname is lastname
You can try like this String[] vals = "Hello??Man?HowAreYou????".split("\\?+"); System.out.println(vals[0]+vals[1]+vals[2]); OUTPUT HelloManHowAreYou But as aname=????? you want to get you can replace the ????? Five Question Marks with Other Symbol and replace back to ????? after split String processed="Hello????Good? ? ....???".replace("????","*"); OUTPUT Hello*Good? ? ....??? And than use split for ?
Here the code, you are looking . Implemented using the Split and HashMap. Tested and Executed. import java.util.HashMap; import java.util.Map; public class Sample { public static void main(String[] args) { // TODO Auto-generated method stub // String[] vals = "Hello??Man?HowAreYou????".split("\\?+"); // System.out.println(vals[0]+vals[1]+vals[2]); 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(" ", "")); } } }
I assume you are parsing URLs. The correct way would be to encode all special characters like ?, & and = which are values or names. Better Solution: Encoding characters: String name = "=p=="; String aname = "aname=????"; String lname = "lastname"; String url = "name=" + URLEncoder.encode(name, "UTF-8") + "?aname=" + URLEncoder.encode(aname, "UTF-8") + "?lname=" + URLEncoder.encode(lname, "UTF-8"); After that you have something like this: name==p==?aname=?????lname=lastname This can be splitted and decoded easily. Other Solution: Bad input parsing: If you insist, this works also. You can use a regex: Pattern pattern = Pattern.compile("(\\w+?)=(\\S+?\\?+)"); Matcher m = pattern.matcher(query + "?"); while (m.find()) { String key = m.group(1); String value = m.group(2); value = value.substring(0, value.length() - 1); System.out.println(key + " = " +value); }