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.
Related
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.
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
I have One String
which has value
String someString = "/category/subcategory/Fruit/apple/";
I want to separate "Fruit" from string.
Make an Array of Strings by splitting with / character like:
String[] split = someString.split("/");
This Array split[] has all the elements separated. You can use whichever you want.
String[] values = someString.split("/");
Log.i(TAG, values[3]);
I have to separate a string into an array that may contain empty spaces, for example,
|maria|joao|fernando||
but it is ignored when you have space at the end of the line
I am using this regex split("\\|")
It should be like it: maria,joao,fernando,null
but stays like this: maria,joao,fernando
You can use:
String str = "|maria|joao|fernando||";
String[] tokens = str.replaceAll("^[|]|[|]$", "").split("[|]", -1));
//=> [maria, joao, fernando, ""]
Steps:
Replace starting and ending | using replaceAll method to get maria|joao|fernando| as input to split.
Then split it using split method with 2nd parameter as -1 to return empty tokens as well.
You only need to add double backslashes to you split string
String s = "|maria|joao|fernando||";
String [] st =s.split("\\|");
for (String string : st) {
System.out.print(string+",");
}
Java 8 solution
List<String> params = Pattern
.compile("\\|")
.splitAsStream("|maria|joao|fernando||")
.filter(e -> e.trim().length() > 0) // Remove spaces only or empty strings
.collect(Collectors.toList());
I'm trying to add a string to an ArrayList<String> using .add(string). This works, but it separates the variables using a comma (","). This makes it problematic for me later on when I try to split the string, because some of the sentences contain commas.
How do I change the comma to another variable when adding a string to an ArrayList?
What you are doing is using the toString method of the ArrayList which will print each of the element of that ArrayList separated with commas.
solution:
just iterate to all of the Arraylist and append them using the String Builder.
sample:
ArrayList<String> s= new ArrayList<>();
s.add("adsasd");
s.add("adsasd");
s.add("adsasd");
s.add("adsasd");
StringBuilder s2 = new StringBuilder();
for(String s3 : s)
s2.append(s3+" ");
System.out.println(s2);
result:
adsasd adsasd adsasd adsasd
let ArrayList str contain the list.
String myString = str.toString();
myString.replaceAll(",","something");
Also you can use String Builder.
You have a ArrayList. I suppose you are doing something with the particular index of the arrayList. So, get that index and then get the String and split it using substring by getting the index of ",". But it this method is not that convenient as your code will be long.