aspose file tools*
The moose likes Java in General and the fly likes How to remove spaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to remove spaces" Watch "How to remove spaces" New topic
Author

How to remove spaces

adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Hi.
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
alexander
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3218
    
  49

java.lang.String class has a replaceAll method. Have you checked it?


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11477
    
  16

basically, you need to come up with a well-defined set of rules for which spaces should go, and which should stay. Once you have that established, the rest is easy.

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...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Another way to do it would be to split the String on the ",", trim each element of the resulting String[] and rewrite the values to a comma separated String.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Thanks for your replies guyz.
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
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11477
    
  16

You need to learn about regular expressions - and you need to better define your problem. Instead of replacing "space-comma-space" with "comma", you actually want to remove

"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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

I read your opening post, and got an immediate deja-vu: http://www.coderanch.com/t/451657/Java-General-intermediate/java/trim-method-help

In short: please Use One Thread Per Question.


Since this thread is more active, I'm closing the other one.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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.
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Hi Garrett.
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:
>
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18989
    
  40

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I still think you are overthinking it.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Commons' StringUtils.join(foo, bar)
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
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
>
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18989
    
  40

I am getting 1 error i.e 'cannot find symbol method split(java.lang.String)'. Please help me out


Take a look at your code again -- I think you may have accidentally substituted a number one, for the lower case el.

Henry
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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.
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
I dint get you Garrett. I am a bit confused, for doing a simple operation why i have to change the code the code below works just fine instead it does not work only when i dont give the space before and after the comma. So isnt there any method to do that.

>
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Thanks a lot guyz special thanks to Garrett. I got what i needed see it bellow i splitted first the string with just a comma. Thanks again
>
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Hi.
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


Output:

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...
>
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
What is your desired output. From your first post I thought that what you wanted was a String containing the comma separated output without leading and trailing spaces. i.e.

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?
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Thanks for your help Garrett. I got the program working now i have trimmed the values before splitting them. Heres the code. If any thing better than this please assist me.

import java.sql.*;
public class test
{
public test()
{
String a="porsche car , ferrari car, yamaha bike ,honda bike , ninja bike";
String[] b=a.split(",\\s?+");
String[] c=null;
for(int i=0;i<b.length;i++)
{
b[i]=b[i].trim();
System.out.println("Value in B: "+b[i]);
c=b[i].split("\\s+");
for(int j=0;j<c.length;j++)
{
System.out.println("Value in C: "+c[j]);
}
}
}
public static void main(String args[])
{
new test();
}
}
>
 
 
subject: How to remove spaces