• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to remove spaces

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.lang.String class has a replaceAll method. Have you checked it?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read your opening post, and got an immediate deja-vu: https://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.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
>
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still think you are overthinking it.

 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Commons' StringUtils.join(foo, bar)
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
>
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic