You can use this :
String str = "A, Category, \"Agriculture, forestry and fishing\",";
String result = str.replaceAll(" (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", "");
//------------------------------^--------------------------------------
System.out.println(result);
This will print :
A,Category,"Agriculture, forestry and fishing",
//----------------------^---------------------
The space inside the quotes is not changes, just outside the quotes.
Here is a code DEMO and here is a regex DEMO
EDIT
This part is from @Exception_al so he suggest to use this :
String result = str.replaceAll("(\\s*,\\s*)(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", ",");
This solution is very good if you want to replace all spaces between comma , and the word.
You can check how this can work in regex DEMO
Answer from Youcef LAIDANI on Stack OverflowTo remove the ", " part which is immediately followed by end of string, you can do:
str = str.replaceAll(", $", "");
This handles the empty list (empty string) gracefully, as opposed to lastIndexOf / substring solutions which requires special treatment of such case.
Example code:
String str = "kushalhs, mayurvm, narendrabz, ";
str = str.replaceAll(", $", "");
System.out.println(str); // prints "kushalhs, mayurvm, narendrabz"
NOTE: Since there has been some comments and suggested edits about the ", $" part: The expression should match the trailing part that you want to remove.
- If your input looks like
"a,b,c,", use",$". - If your input looks like
"a, b, c, ", use", $". - If your input looks like
"a , b , c , ", use" , $".
I think you get the point.
You can use this:
String abc = "kushalhs , mayurvm , narendrabz ,";
String a = abc.substring(0, abc.lastIndexOf(","));
Try with the following code to trim all unwanted comma and whitespaces
String str = "kushal,mayurv,narendra,dhrumil,mark, ,,,, ";
String splitted[] = str.split(",");
StringBuffer sb = new StringBuffer();
String retrieveData = "";
for(int i =0; i<splitted.length; i++){
retrieveData = splitted[i];
if((retrieveData.trim()).length()>0){
if(i!=0){
sb.append(",");
}
sb.append(retrieveData);
}
}
str = sb.toString();
System.out.println(str);
Use regex to solve it. You want to remove all (,) that are followed by space or another (,). You also want to remove all (,) that isn't followed by a letter.
Regex in Android
yourstring = yourstring.replaceAll("( ,)|(,,)", "");
Something like that, sorry that I can't help you more.
Use regular expression \s*,\s* for splitting.
String result[] = attributes.split("\\s*,\\s*");
For Initial and Trailing Whitespaces
The previous solution still leaves initial and trailing white-spaces. So if we're expecting any of them, then we can use the following solution to remove the same:
String result[] = attributes.trim().split("\\s*,\\s*");
Using java 8 you can do it like this in one line
String[] result = Arrays.stream(attributes.split(",")).map(String::trim).toArray(String[]::new);
You could do this:
String str = "...";
List<String> elephantList = Arrays.asList(str.split(", "));
In Java 9+, create an unmodifiable list with List.of.
List<String> elephantList = List.of(str.split(", ")); // Unmodifiable list.
Basically the .split() method will split the string according to (in this case) delimiter you are passing and will return an array of strings.
However, you seem to be after a List of Strings rather than an array. So the array must be turned into a list. We can turn that array into a List object by calling either Arrays.asList() or List.of.
FYI you could also do something like so:
String str = "...";
ArrayList<String> elephantList = new ArrayList<>(Arrays.asList(str.split(","));
But it is usually better practice to program to an interface rather than to an actual concrete implementation. So I would not recommend this approach.
Well, you want to split, right?
String animals = "dog, cat, bear, elephant, giraffe";
String[] animalsArray = animals.split(",");
If you want to additionally get rid of whitespaces around items:
String[] animalsArray = animals.split("\\s*,\\s*");