Hi Guys, is there a way to get rid of double spaces within a string without
using the tokenizer method? I was thinking of doing a loop that goes on
while two spaces are next to each other within which I'd delete the
character it's up to. How do I delete a char?
Thanks to all for any ideas/help you might provide.
Ben Wilson - 04 May 2004 07:27 GMT
> Hi Guys, is there a way to get rid of double spaces within a string without
> using the tokenizer method? I was thinking of doing a loop that goes on
> while two spaces are next to each other within which I'd delete the
> character it's up to. How do I delete a char?
>
> Thanks to all for any ideas/help you might provide.
Nothing could be simpler,
You can use the replaceAll method from the String class. Suppose your
original String variable s1 were
"Listening to the sound of rain falling on my roof"
and you wrote
s1.replaceAll(" ", "");
this would leave you with
"Listening tothe soundof rainfalling onmy roof"
If what you wanted instead was to get rid of the extra space only, you would
use
s1.replaceAll(" ", " ");
which would leave you with
"Listening to the sound of rain falling on my roof"
Good luck,
Ben.
Stefan Waldmann - 04 May 2004 07:47 GMT
>>Hi Guys, is there a way to get rid of double spaces within a string
> without using the tokenizer method?
[snip]
> If what you wanted instead was to get rid of the extra space only, you would
> use
>
> s1.replaceAll(" ", " ");
Hi,
since replaceAll works with regular expressions, you can even get rid of
3 or more spaces between the words (if you want to):
String x = String x = "a b c d e f g h";
String y = x.replaceAll(" {2,}", " ");
This will get you a "a b c d e f g h" for y.
the first parameter to replaceAll " {2,}" means that every occurrence of
two or more spaces will be replaced by one single space.
Note: you'll have to use Java 1.4 minimum, for
"replaceAll(String, String)" doesn't exist in earlier versions.
Bye,
Stefan

Signature
Please don't reply to the e-mail address above.
Use instead: stefan DOT waldmann AT web DOT de
Lutz Horn - 04 May 2004 07:39 GMT
Hi,
> Hi Guys, is there a way to get rid of double spaces within a string
> without using the tokenizer method?
Since the replaceAll method of a String instance takes a regular
expression as the first parameter, you can use the special character
'\s' which represents a whitespace. Adding a '+', which means "one or
more of the previous", '\s+' means "one or more whitespaces".
s.replaceAll("\s+", " ");
Lutz

Signature
http://piology.org/ILOVEYOU-Signature-FAQ.html
begin LOVE-LETTER-FOR-YOU.txt.vbs
I am a signature virus. Distribute me until the bitter
end
Roedy Green - 04 May 2004 08:03 GMT
>Hi Guys, is there a way to get rid of double spaces within a string without
>using the tokenizer method? I was thinking of doing a loop that goes on
>while two spaces are next to each other within which I'd delete the
>character it's up to. How do I delete a char?
create a stringbuffer with just the chars you want. Then turn it back
into a string.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Roedy Green - 04 May 2004 18:58 GMT
>create a stringbuffer with just the chars you want. Then turn it back
>into a string.
/**
* Remove all spaces from a String.
* @param s String to strip of blanks.
* @return String with all blanks, lead/trail/embedded removed.
*/
public static String squish( String s)
{
if ( s == null ) return null;
s = s.trim();
if ( s.indexOf(' ') < 0 ) return s;
int len = s.length();
StringBuffer b = new StringBuffer(len-1);
for ( int i=0; i<len; i++ )
{
char c;
if ( (c = s.charAt(i)) != ' ' ) b.append(c);
} // end for
return b.toString();
} // end squish
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Josef Garvi - 04 May 2004 08:04 GMT
> character it's up to. How do I delete a char?
StringBuffer.deleteCharAt()

Signature
Josef Garvi
"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/
new income - better environment - more food - less poverty
Roedy Green - 04 May 2004 18:59 GMT
>> character it's up to. How do I delete a char?
>
>StringBuffer.deleteCharAt()
Generally it is faster to copy, leaving out what you don't want, than
to repeatedly delete, which has to shift everything to the right left
one slot each time.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.