How to Ignore the desired string during the split in Java? - java

I have a string like
pchase_history:array<struct<pchase_channel:string,trans_dt:string,sku_id:string,sold_qty:bigint>>,first_pchase_dt:string,last_pchase_dt:string,trans_cnt:bigint,last_pchase_sku_cnt:bigint,no_of_pchase_days:bigint,lst_pchase_channel:array<struct<pchase_channel:string>>
and i need to split it by ',' but don't want to split (array of struct) array<struct<pchase_channel:string,trans_dt:string,sku_id:string,sold_qty:bigint>>
I want split method to ignore these array of struct and split the rest of the string.
How can i achieve this by split method?
Any help would be appreciated.

You can use a regex to replace your array of struct before doing split like this:
String value = "pchase_history:array<struct<pchase_channel:string,trans_dt:string,sku_id:string,sold_qty:bigint>>,first_pchase_dt:string,last_pchase_dt:string,trans_cnt:bigint,last_pchase_sku_cnt:bigint,no_of_pchase_days:bigint,lst_pchase_channel:array<struct<pchase_channel:string>>";
value = value.replaceAll("(array<struct<.*?>>)", "array");
String[] splitedValues = value.split(",");
System.out.println(Arrays.toString(splitedValues));
Output:
[pchase_history:array, first_pchase_dt:string, last_pchase_dt:string, trans_cnt:bigint, last_pchase_sku_cnt:bigint, no_of_pchase_days:bigint, lst_pchase_channel:array]
Click here to test regex online

Related

Get two different delimiters from same string

How can I use split function in java using two delimiters in the same string
I want to get the words with commas and spaces separately
String I = "hello,hi hellow,bye"
I want to get the above string splited as
String var1 = hello,bye
String var2 = hi hellow
Any suggestion is very much valued.
I would try to first split them with one of the delimiters, then for each resulting substring split with the other delimiter.

How to split a string with numbers in java

I have string "ali22mehdi35abba1lala2". I want to split it to {"ali","mehdi","abba","lala"};
How should I do that ?
I saw here and another place. I can't acheive my end.
Try following code
String string="ali22mehdi35abba1lala2";
String tok[]=string.split("\\d+");
Now tok would have the split array from numbers.
Use this:
String[] phNo = "ali22mehdi35abba1lala2".split("\\d+");

convery string into array with brackets

I have the following String
[http://images.com/1.jpg, http://images.com/2.jpg, http://images.com/3.jpg]
I want to store the contents of this array inside a string array or array list of type string.
I tried using .split method, but it fails mainly because the string also contains the brackets at the beginning.
String[] splittedString = theString.substring(1, theString.length()-1).split(", ")
Notice space after comma in the split method.
Use substring to exclude the string from the brackets:
mystring = mystring.substring(1,mystring.length()-1);
And then the split:
String[] myarray = mystring.split(", ");
String arry[] = yourstr.replace("[", "").replace("]", "").split(",");
Escape the first and the last characters of your String containing the brackets before using the split() method, like this :
yourString= yourString.substring(1, yourString.length()-1));
// do your split() method
Optionally you can use StringTokenizer.

Java: String splitting into multiple elements

I am having a difficult time figuring out how to split a string like the one following:
String str = "hi=bye,hello,goodbye,pickle,noodle
This string was read from a text file and I need to split the string into each element between the commas. So I would need to split each element into their own string no matter what the text file reads. Keep in mind, each element could be any length and there could be any amount of elements which 'hi' is equal to. Any ideas? Thanks!
use split!
String[] set=str.split(",");
then access each string as you need from set[...] (so lets say you want the 3rd string, you would say: set[2]).
As a test, you can print them all out:
for(int i=0; i<set.length;i++){
System.out.println(set[i]);
}
If you need a bit more advanced approach, I suggest guava's Splitter class:
Iterable<String> split = Splitter.on(',')
.omitEmptyStrings()
.trimResults()
.split(" bye,hello,goodbye,, , pickle, noodle ");
This will get rid of leading or trailing whitespaces and omit blank matches. The class has some more cool stuff in it like splitting your String into key/value pairs.
str = str.subString(indexOf('=')+1); // remove "hi=" part
String[] set=str.split(",");
I'm wondering: Do you mean to split it as such:
"hi=bye"
"hi=hello"
"hi=goodbye"
"hi=pickle"
"hi=noodle"
Because a simple split(",") will not do this. What's the purpose of having "hi=" in your given string?
Probably, if you mean to chop hi= from the front of the string, do this instead:
String input = "hi=bye,hello,goodbye,pickle,noodle";
String hi[] = input.split(",");
hi[0] = (hi[0].split("="))[1];
for (String item : hi) {
System.out.println(item);
}

How do I fill a new array with split pieces from an existing one? (Java)

I'm trying to split paragraphs of information from an array into a new one which is broken into individual words. I know that I need to use the String[] split(String regex), but I can't get this to output right.
What am I doing wrong?
(assume that sentences[i] is the existing array)
String phrase = sentences[i];
String[] sentencesArray = phrase.split("");
System.out.println(sentencesArray[i]);
Thanks!
It might be just the console output going wrong. Try replacing the last line by
System.out.println(java.util.Arrays.toString(sentencesArray));
The empty-string argument to phrase.split("") is suspect too. Try passing a word boundary:
phrase.split("\\b");
You are using an empty expression for splitting, try phrase.split(" ") and work from there.
This does nothing useful:
String[] sentencesArray = phrase.split("");
you're splitting on empty string and it will return an array of the individual characters in the string, starting with an empty string.
It's hard to tell from your question/code what you're trying to do but if you want to split on words you need something like:
private static final Pattern SPC = Pattern.compile("\\s+");
.
.
String[] words = SPC.split(phrase);
The regex will split on one or more spaces which is probably what you want.
String[] sentencesArray = phrase.split("");
The regex based on which the phrase needs to be split up is nothing here. If you wish to split it based on a space character, use:
String[] sentencesArray = phrase.split(" ");
// ^ Give this space

Categories