I want to remove the spaces from a string. Not all spaces, if i use trim() method all spaces are removed. I need few spaces bet ween the values. example "ferrari car , apple fruit , potato veg". Now is it possible to remove spaces which are before and after the commas. If so how please help.
Thanks and Regards
it SOUNDS like what you want is to replace every occurrence of " , " (space-comma-space) with "," (comma). Note that regular expressions in java can be a little tricky, but it should be able to do what you want...
Fred, what you understood is wright, i need to remove the spaces before and after the comma, but user can even enter two or more spaces before or after the comma or can even enter to or more tabs just like this: ferrari car , apple fruit. Then what shall i do in such a case. I want to remove just the spaces before and after the comma. I have even done what Garrett has said, i used the split() method to split with comma. Just like this str.split(",") but the problem here is after i split i get two string the first is: ferrari car and the second is: apple fruit. Now when i use trim() method, because i need the spaces between ferrari car this doesnt suit my requirement, the trim() method remove all the spaces including which i need. Hope you guyz have gotta my problem.
Cooray has said to use the replaceAll() method but how can i use it to solve my problem can you please help.
Thanks and Regards
"zero or more whitespace characters-comma-zero or more whitespace characters"
A correctly formed regular expression (or regex, for short) will let you do this without too many difficulties. And fortunately, the String.replaceAll() method accepts a regex as the first parameter!!
of course, that assumes we now have the problem properly defined.
In short: please Use One Thread Per Question.
Since this thread is more active, I'm closing the other one.
adeeb alexander wrote:I have even done what Garrett has said, i used the split() method to split with comma. Just like this str.split(",") but the problem here is after i split i get two string the first is: ferrari car and the second is: apple fruit. Now when i use trim() method, because i need the spaces between ferrari car this doesnt suit my requirement, the trim() method remove all the spaces including which i need
String.trim() does not do what you say it does. It only removes the leading and trailing whitespace, not the whitespace between words.
public String trim()
Returns a copy of the string, with leading and trailing whitespace omitted.
If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned.
Otherwise, if there is no character with a code greater than '\u0020' in the string, then a new String object representing an empty string is created and returned.
I have tried what have you said but made a slight change, i used '\s' instead of '\u0020'. It works even if i give many spaces and tabs before and after the comma(,). Now the problem is that i get a different output if i dont give any space between word and comma. You can better understand if you run the code below with out the space between the word and comma just like this: String a="porsche car,ferrari car";. The code is Here:
Now the problem is that i get a different output if i dont give any space between word and comma.
That's because you used the "+" qualifier -- which means one or more of the previous. If you mean zero or more of the previous, then you should use the "*" qualifier.
adeeb alexander wrote:Thanks for your reply Garrett.
Can you please tell me where i am going wrong now. I am getting 1 error i.e 'cannot find symbol method split(java.lang.String)'. Please help me out
I don't think you are getting what I'm trying to convey. What do you think this method should do? In words, not in code.
When i split the first string with just a comma amd the remaining asusual every thing works fine but i get a white space as a string. the code and output are below
I mean that if i remove just the trailing space its enough. How can i do that
Value in B: porsche car
Value in C: porsche
Value in C: car
Value in B: ferrari car
Value in C:
Value in C: ferrari
Value in C: car
Value in B: yamaha bike
Value in C:
Value in C: yamaha
Value in C: bike
Press any key to continue...
input = "porsche car , ferrari car, yamaha bike"
output = "porsche car,ferrari car,yamaha bike"
Apparently this is not the case. What output are you looking for?
public class test
String a="porsche car , ferrari car, yamaha bike ,honda bike , ninja bike";
System.out.println("Value in B: "+b[i]);
System.out.println("Value in C: "+c[j]);
public static void main(String args)