how can i split a string inside curly braces? - java

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];

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

In json Parsing how to set the image set

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);

regex to match and replace two characters between string

I have a string String a = "(3e4+2e2)sin(30)"; and i want to show it as a = "(3e4+2e2)*sin(30)";
I am not able to write a regular expression for this.
Try this replaceAll:
a = a.replaceAll("\) *(\\w+)", ")*$1");
You can go with this
String func = "sin";// or any function you want like cos.
String a = "(3e4+2e2)sin(30)";
a = a.replaceAll("[)]" + func, ")*siz");
System.out.println(a);
this should work
a = a.replaceAll("\\)(\\s)*([^*+/-])", ") * $2");
String input = "(3e4+2e2)sin(30)".replaceAll("(\\(.+?\\))(.+)", "$1*$2"); //(3e4+2e2)*sin(30)
Assuming the characters within the first parenthesis will always be in similar pattern, you can split this string into two at the position where you would like to insert the character and then form the final string by appending the first half of the string, new character and second half of the string.
string a = "(3e4+2e2)sin(30)";
string[] splitArray1 = Regex.Split(a, #"^\(\w+[+]\w+\)");
string[] splitArray2 = Regex.Split(a, #"\w+\([0-9]+\)$");
string updatedInput = splitArray2[0] + "*" + splitArray1[1];
Console.WriteLine("Input = {0} Output = {1}", a, updatedInput);
I did not try but the following should work
String a = "(3e4+2e2)sin(30)";
a = a.replaceAll("[)](\\w+)", ")*$1");
System.out.println(a);

Usage of the java function 'split()

I would like to split the word in java based on the delimeter'-'when it appeared last.
I am expecting the result as "sweet_memory_in" and "everbodylife#gmail.com". Do we have any inbuilt function in java.
Complete word is sweet_memory_in_everbodylife#gmail.com
String s = "sweet_memory_in_everbodylife#gmail.com";
String first = s.substring(0,s.lastIndexOf("_"));
String second = s.substring(s.lastIndexOf("_")+1 );
try this
String s = "sweet_memory_in_everbodylife#gmail.com";
String s1 = s.substring(0,s.lastIndexOf("_"));
String s2 = s.substring(s.lastIndexOf("_")+1,s.length());
Regex may help. Other way is to get the last index of _ and use substring to split it.
Try out this code :
String data = "sweet_memory_in_everbodylife#gmail.com";
int lastIndex = data.lastIndexOf("_");
String firstSplit = data.substring(0, lastIndex);
String secondSplit = data.substring(lastIndex + 1, data.length());
System.out.println(firstSplit);
System.out.println(secondSplit);
Try this my friend (Javascript Codes):
var str = 'sweet_memory_in_everbodylife#gmail.com';
var arr1 = str.substring(str.lastIndexOf("_")).split("_");
var arr2 = str.split("_"+arr1[1]);
alert(arr2[0] +" --> "+arr1[1]);

Parse string starting from the end

I want to split string around only the last _ character , example:some_string_foo_bar into two substrings some_string_foo bar.
I tried to use Pattern and StringTokenizer, but they always start from the beginning of stirng. Any idea how to do this?
Use lastIndexOf; there's no reason to do a full split.
Sure, this might be of some use. Here's an example.
String source = "hello_world_foo";
int pos = source.lastIndexOf('_');
String before = source.substring(0, pos);
String after = source.substring(pos + 1);
You can use:
String strX = "some_string_foo";
String str1 = strX.substring(0,strX.lastIndexOf("_"));
String str2 = strX.subscting(strX.lastIndexOf("_"),strX.length());
String[] arr = str.split("_");
String lastOne = arr[arr.length-1];

Categories