i have a string array like
String[] resultYears = {"2013/07", "2013/08", "2013/09", "2013/10", "2013/11", "2013/12", "2014/01"};
when i get this through response in my jsp,
<% String[] resultYears = (String[]) request.getAttribute("resultYears");%>
i am getting the remainders of their respective values. How can i get same as above string value?
Try this way in your JAVA Class
String[] resultYears = {"'2013/07'", "'2013/08'", "'2013/09'", "'2013/10'", "'2013/11'", "'2013/12'", "'2014/01'"};
request.setAttribute("resultYears", resultYears);
Try the strings using single quotes like this
"'2013/08'"
How about instantiating a Date object with the YYYY/MM desired and do a .getTime() on it, then pass that through the request in String value. On the far end, translate it back into a pretty, human readable format.
Related
I'm trying to convert JSON object into string by doing the below
JSONObject object = new JSONObject();
object.put("video", data);
array1.add( object.toString().replace("\\\\"," "));
Actual result
["{\"photos\":\"/contests/1/images/1.png\"}",
{\"photos\":\"/contests/1/images/2.png\"}"]
Expected result
["{"photos":"/contests/1/images/1.png\"}","
{"photos":"/contests/1/images/2.png\"}"]
not able to remove the slashes from key
Use replaceAll instead of replace
replaceAll("\\\\", "")
When you want to replace all the occurences with .replace, the first parameter must be a regex, if you supply a string, only the first occurrence will be replaced, that's why your replace wouldn't work.
Please use:
.replace("/\\/g","")
Alternatively, replaceAll can be used as #Code_Mono suggested
The Code_Mode mentioned is correct one.
Because String is immutable. Make sure that you put it right place.
You can refer code bellow for more detail:
public static void main(String[] args) {
String json = "[\"{\\\"photos\\\":\\\"/contests/1/images/1.png\\\"}\", {\\\"photos\\\":\\\"/contests/1/images/2.png\\\"}\"]";
json.replaceAll("\\\\","");
System.out.println(json);
String jsonReplace = json.replaceAll("\\\\","");
System.out.println(jsonReplace);
}
Output value:
["{\"photos\":\"/contests/1/images/1.png\"}", {\"photos\":\"/contests/1/images/2.png\"}"]
["{"photos":"/contests/1/images/1.png"}", {"photos":"/contests/1/images/2.png"}"]
Not sure how it's working in java.
In c# i used to use index of and substring.
private String[] ipaddresses = new String[]{
"http://10.0.0.4:8098/?cmd=nothing",
"http://10.0.0.3:8098/?cmd=nothing"};
private String iptouse = "";
I want to assign to iptouse the first index of the array ipaddresses but only part of the index:
iptouse = "http://10.0.0.4:8098/?cmd=" + "start";
It's only example what i want is to extract from the sting in index 0 only this part: http://10.0.0.4:8098/?cmd=
You can process it with String class or with Apache Commons StringUtils (they have a handful of cool methods for text parsing).
However, the propper way would be to use Java URI class.
There you have a very handy methods for extracting the host and the query part etc. To understand it better read about the structure of URI here
In your case it would be something like:
URI u = new URI("http://10.0.0.4:8098/?cmd=nothing");
String upToQuery = u.getScheme()+u.getAuthority()+u.getPath();
It's the same in Java use substring() and indexOf:
String iptouse = ipaddresses[0].substring(0,ipaddresses[0].indexOf('=')+1)+"start";
String symbol="=";
String iptouse =ipaddresses[0].split(symbol)[0]+symbol+"start";
Or
replaceFirst("nothing", "start");
I have a code to replace stream of string. I need to search a specific string that is defined in the key of properties file
String result="";
int i=0;
while (i<listToken.size()){
result = listToken.get(i);
while (enuKey.hasMoreElements()) {
String key = (String)enuKey.nextElement();
// String value = propertiesSlang.getProperty(key);
if (listToken.get(i).equals(key)){
String value = propertiesSlang.getProperty(key);
listToken.get(i).replace(listToken.get(i), value);
System.out.print("detected");
}
}
i++;
}
But it doesn't replace word. How I can replace words using properties.
It's because you forgot to assign the result, using the method set():
listToken.set(i, propertiesSlang.getProperty(key)));
assuming listToken implements AbstractList
Why complicate things with replace(). As far as I understand your code you can simply do -
String value = propertiesSlang.getProperty(key);
listToken.set(i, value);
I see you have modified your code again to
listToken.get(i).replace(listToken.get(i), value);
Just so that you know String class is immutable. So operations like replace() or substring() will give you a new String and not modify the original one. Get the new String and set it in your list listToken.
I receive a string from an http request which contains data such as:
{"status":1,"Type":3,"Data":"<p style=\"padding-left:80px\"><\/p><ol><li><span style=\"color:#ff0000\">This<\/span><\/li><li>is a<\/li><li><strong>m<span style=\"background-color:#66cc00\">are<\/span><\/strong><\/li><\/ol><p><\/p><p style=\"padding-left:80px\"><strong style=\"text-align:left\"><span style=\"background-color:#66cc00\"><\/span><\/strong><\/p> "}
I convert it to a JSONObject like so:
jsonObj = new JSONObject(result);
I then need to get the html as a String to display in a TextView,
I have tried this:
String data = jsonObj.getString("data");
but data remains null. This works with simple json strings, but i think it might be cause of the " characters.
You are using "data" with a lowercase d, but your JSON contains "Data" with a capital D. Use this:
jsonObj.getString("Data");
You have used "data" instead of "Data". This is the only silly mistake you did. To avoid such type of typo mistake, always user final static String to access them from anywhere.
final static String KEY_DATA = "Data";
Then access it inside your class (suppose class name is Aclass):
jsonObj.getString(KEY_DATA);
And in other classes:
jsonObj.getString(Aclass.KEY_DATA);
This is a good practice indeed and no possibility of typo mistake!
I have a string query which looks like this
{"query":{"bool":{"should":[{"terms":{"user.id":[#users_to_follow],"minimum_match":1}},{"terms":{"tweets":[#keywords_to_track],"minimum_match":1}}]}},"filter":{"range":{"publishedDate":{"from":#Unix_timestamp}}},"size":#sizelength}"
I am trying to replace certain strings in the query with another string using:
query.replace("#users_to_follow",usersToFollow);
query.replace("#keywords_to_track", keyworsToTrack);
query.replace("#Unix_timestamp","1325930428000" );
query.replace("#sizelength",Integer.toString(SMLApplicationProperties.ES_RESULTSET_SIZE));
However when I run this thing, it does not actually replace the given strings from query.
You need to understand that String class is immutable. Any operation on String returns a new String. You need to assign it back to query variable:
query = query.replace("#users_to_follow",usersToFollow);
query = query.replace("#keywords_to_track", keyworsToTrack);
query = query.replace("#Unix_timestamp","1325930428000" );
query = query.replace("#sizelength",Integer.toString(SMLApplicationProperties.ES_RESULTSET_SIZE));
You need to get the result of the replace operation :
query = query.replace("#users_to_follow",usersToFollow);
That's because query.replace doesn't change the string you pass, it builds and returns a new one (string in java are immutable).
You can optimize (not accepted by everybody) :
String query = query.replace("#users_to_follow",usersToFollow).replace("#keywords_to_track", keyworsToTrack).replace("#Unix_timestamp","1325930428000" ).replace("#sizelength",Integer.toString(SMLApplicationProperties.ES_RESULTSET_SIZE));