In json Parsing how to set the image set - java

In this json i have two image path
String imgUrl="http://eonion.in/vimalsagarji/static/eventimage/abc.jpg"
and second
String imgPhoto="Jellyfish82238.jpg" so I want the first String in this abc.jpg replcae with Jellyfish82238.jpg and parse in JSON so please help me.

We assume that the URL is dynamic in every time. We assume also that the base URL to get images is
http://eonion.in/vimalsagarji/static/eventimage/
In this case, we could play with split to perform what you want
String imgUrl="http://eonion.in/vimalsagarji/static/eventimage/abc.jpg"
String[] parts = string.split("/");
So, then we have :
String part1 = parts[0]; // http://eonion.in
String part2 = parts[1]; // vimalsagarji
String part3 = parts[2]; // static
String part4 = parts[3]; // eventimage
String part5 = parts[4]; // abc.jpg
Then, we replace part5 by imgPhoto.
kString newImageUrl = part1 + "/" part2 + "/" part3 + "/" + part4 + "/" + imgPhoto

Simply use replace()
String imgUrl="http://eonion.in/vimalsagarji/static/eventimage/abc.jpg";
String imgPhoto="Jellyfish82238.jpg";
String url= imgUrl.replace("abc.jpg", imgPhoto);

Simply use replace(). Since replace() will produce a new String, you should assign the new String to imgUrl
imgUrl= imgUrl.replace("abc.jpg", imgPhoto);

Try this:
String imgUrl="http://eonion.in/vimalsagarji/static/eventimage/abc.jpg";
String imgPhoto="Jellyfish82238.jpg";
imgUrl=imgUrl.substring(0,imgUrl.lastIndexOf("/")+1)+imgPhoto;
Log.d("imagevalueclicked",imgUrl);

Related

Splitting a string at a defined sign - especially "("

I have a little problem with splitting a String
String anl_gewerk = "Text Text Text (KG 412/2)"
String[] parts = anl_gewerk.split("[(]");
anl_gewerk = parts[1];
anl_gewerk = anl_gewerk.replaceAll("\\(","").replaceAll("\\)","");
anl_gewerk = anl_gewerk.replace("KG","");
I have the aforementioned string, and I'm searching for "412/2".
Therefore, I want to split the String into two substrings searching for "(".
Finally I want to grab this String deleting "(", "KG", " " and ")".
When I select anl_gewerk = parts[0]; it works but I get the wrong part, when I change into parts[1] the App crashes.
Please help me
Try to change your code by this:
String anl_gewerk = "Text Text Text (KG 412/2)";
String[] parts = anl_gewerk.split("[(]");
anl_gewerk = parts[1];
String sub[] = anl_gewerk.split(" ");
String test = sub[1];
String result = test.replace(")","");// it gives your result 412/2

Java, Substring to get value from string

given a string like this
string a = "Course Name:\n" + CourseName
+"\n\nVenue:\n" + locationName
+"\n\nStart Time:\n" + startTime
+"\n\nEnd Time:\n"+ endTime";
CourseName,locationName,startTime and endTime would be a variable and being assigned to string a, how can i get all these value from the string using substring or regex and store all of it into different variable? please note that I don't have access all these variable, I can only access the string which leave me the only option is play around with substring or regex.
You could split by newline
String[] parts = a.split("\\n");
String courseName = parts[1];
String locationName = parts[4];
String startTime= parts[7];
String endTime= parts[10];
or you can use:
StringTokenizer tokens=new StringTokenizer(a, "\n");
while(tokens.hasMoreTokens()){
System.out.println(tokens.nextToken());
}

how can i split a string inside curly braces?

I have a string which looks like this
[{"TransactionId":"3574780600039252015-12-24 T 14:22:03"
Now I want to split only the text where the "TransactionId" will be part one and after : will be the second part.
Code I've tried :
String[] transid_result = result.split(":");
String part1 = transid_result[0];
String part2 = transid_result[1];
The result is
part1 contains [{"TransactionId
part2 contains "3574780600039252015-12-24 T 14
I want part2 to contain "3574780600039252015-12-24 T 14:22:03"
Can anyone help me ?
You could search for the first : and manually split by that:
int firstColon = result.indexOf(":");
String part1 = result.substring(0,firstColon);
String part2 = result.substring(firstColon+1, result.length());
If you're sure the data will always be in this format, then you could use
String[] elements = result.split("\"");
String transactionId = elements[3];

String replace using replace first method

I was trying to use the string replace with another, it doesn't happen.
String requestURI = "/webapps-ab/public/Test.jsp"
String contextName = "webapps-ab";
String newRequestURI = requestURI.replaceFirst(contextName,"webapps");
I am expecting newRequestURI to be "/webapps/public/Test.jsp".
Your replace call should be:
String newRequestURI = requestURI.replaceFirst(contextName, "webapps");
Using:
String requestURI = "/webapps-ab/public/Test.jsp";
String contextName = "webapps-ab";
String newRequestURI = requestURI.replaceFirst(contextName, "webapps");
System.out.println("newRequestURI: " + newRequestURI);
The output will be what you're expecting:
newRequestURI: /webapps/public/Test.jsp
ideone example.
When referencing a variable, do not surround it in quotes, as that converts it into a literal String object.
String newRequestURI = requestURI.replaceFirst(contextName, "webapps");

Making a song name out of a URL

I have a URL and I want it to look like this:
Action Manatee - Action
http://xxxxxx.com/songs2/Music%20Promotion/Stream/Action%20Manatee%20-%20Action.mp3
What is the syntax for trimming up to where it after this "Stream/" and make spaces where the %20 is. I also want to trim the .mp3
Hmm, for that particular example, I would split the string according to the '/' character then trim the text that follows the final '.' character. Finally, do a replace of "%20" into " ". That should leave you with the string you want
Tested
String initial = "http://xxxxxx.com/songs2/Music%20Promotion/Stream/Action%20Manatee%20-%20Action.mp3";
String[] split = initial.split("/");
String output = split[split.length-1];
int length = output.lastIndexOf('.');
output = output.substring(0, length);
output = output.replace("%20", " ");
String urlParts[] = URL.split("\/");
String urlLast = urlParts[length-1];
String nameDotMp = urlLast.replaceAll("%20");
String name = nameDotMp.substring(0,nameDotMp.length-5);
You could use the split() and replace() methods to accomplish this, here are two ways:
Split your string apart by using the forward slashes:
string yourUrl = [URL Listed];
//Breaks your URL into sections on slashes
string[] sections = yourUrl.split("\/");
//Grabs the last section after the slashes, and replaces the %20 with spaces
string newString = sections[sectiongs.length-1].replace("%20"," ");
Split your string at the Stream/ section: (Only use this if you can guarantee it will be in that form)
string yourUrl = [URL Listed];
//This will get everything after Stream (your song name)
string newString = yourUrl.split("Stream\/")[1];
//Replaces your %20s with spaces
newString = newString.replace("%20"," ");
URL songURL = new URL("yourpath/filename");
String filename = songURL.getFile();

Categories