Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > String consisting of spaces

Reply
Thread Tools

String consisting of spaces

 
 
Andrew Smith
Guest
Posts: n/a
 
      10-17-2003
I have a string, s, and I want another string, t, which has s.length() ==
t.length() but t only consists of white space ?

Is there a faster way than this.

String t = ""; // (or stringbuffer if t is big enough to make it worthwhile)
for (int n=0;n < s.length(); n++) {
t += " ";
}




 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      10-17-2003

"Andrew Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I have a string, s, and I want another string, t, which has s.length() ==
> t.length() but t only consists of white space ?
>
> Is there a faster way than this.
>
> String t = ""; // (or stringbuffer if t is big enough to make it

worthwhile)
> for (int n=0;n < s.length(); n++) {
> t += " ";
> }
>


In a word... YES. I'm afraid your method is very bad.
It will internally create s.length StringBuffers, when one would have done.

> or stringbuffer if t is big enough to make it worthwhile


Yes t is always big enough to make it worthwhile.

But this would be even better. Just primative char[] and your final String:

String s = "myString";

char[] spaces = new char[s.length()];

for(int i = 0 ; i<spaces.length; i++)
spaces[i] = ' ';

String t = new String(spaces);

System.out.println(">"+t+"<");
System.out.println(">"+s+"<");


--
Mike W


 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      10-17-2003
Andrew Smith <(E-Mail Removed)> wrote:
> I have a string, s, and I want another string, t, which has s.length() ==
> t.length() but t only consists of white space ?


> Is there a faster way than this.


If there is a maximum length for s you could preconstruct a String
of that length that contains only spaces and then just extract
a substring of the appropriate length when you need it. May or
may not be faster depending on the usage.

--arne

DISCLAIMER: These opinions and statements are those of the author and
do not represent any views or positions of the Hewlett-Packard Co.
 
Reply With Quote
 
Paul Lutus
Guest
Posts: n/a
 
      10-17-2003
Andrew Smith wrote:

> I have a string, s, and I want another string, t, which has s.length() ==
> t.length() but t only consists of white space ?
>
> Is there a faster way than this.
>
> String t = ""; // (or stringbuffer if t is big enough to make it
> worthwhile) for (int n=0;n < s.length(); n++) {
> t += " ";
> }


Yes, in fact that is extremely inefficient for some technical reasons. This
is much faster:

String original = "This is my test string.";
String replacement = original.replaceAll("."," ");

System.out.println("[" + original + "]");
System.out.println("[" + replacement + "]");
System.out.println(original.length() - replecement.length());

Result:

[This is my test string.]
[ ]
0

Before the purists reply, remember -- the replaceAll() method is written in
native code, and "faster" was the criterion.

--
Paul Lutus
http://www.arachnoid.com

 
Reply With Quote
 
Michiel Konstapel
Guest
Posts: n/a
 
      10-17-2003
"Andrew Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I have a string, s, and I want another string, t, which has s.length() ==
> t.length() but t only consists of white space ?
>
> Is there a faster way than this.
>
> String t = ""; // (or stringbuffer if t is big enough to make it

worthwhile)
> for (int n=0;n < s.length(); n++) {
> t += " ";
> }


If you know the maximum length of s beforehand, this would work:
String spaces = " "; // MAX_LENGTH spaces
t = spaces.substring(0, s.length()); // this may be off by one, I'm never
sure

HTH,
Michiel


 
Reply With Quote
 
VisionSet
Guest
Posts: n/a
 
      10-17-2003

"Paul Lutus" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> This
> is much faster:
>
> String original = "This is my test string.";
> String replacement = original.replaceAll("."," ");
>
> System.out.println("[" + original + "]");
> System.out.println("[" + replacement + "]");
> System.out.println(original.length() - replecement.length());
>
> Result:
>
> [This is my test string.]
> [ ]
> 0
>
> Before the purists reply, remember -- the replaceAll() method is written

in
> native code, and "faster" was the criterion.
>


That's funny, I make your method 25x slower than my char[] one

--
Mike W


 
Reply With Quote
 
Paul Lutus
Guest
Posts: n/a
 
      10-17-2003
VisionSet wrote:

>
> "Paul Lutus" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> This
>> is much faster:
>>
>> String original = "This is my test string.";
>> String replacement = original.replaceAll("."," ");
>>
>> System.out.println("[" + original + "]");
>> System.out.println("[" + replacement + "]");
>> System.out.println(original.length() - replecement.length());
>>
>> Result:
>>
>> [This is my test string.]
>> [ ]
>> 0
>>
>> Before the purists reply, remember -- the replaceAll() method is written

> in
>> native code, and "faster" was the criterion.
>>

>
> That's funny, I make your method 25x slower than my char[] one


That likely includes library loading time, in which case it isn't a fair
comparison. In fact, this is faster if those differences are eliminated.

--
Paul Lutus
http://www.arachnoid.com

 
Reply With Quote
 
Paul Lutus
Guest
Posts: n/a
 
      10-17-2003
Michiel Konstapel wrote:

< snip >

> If you know the maximum length of s beforehand, this would work:
> String spaces = " "; // MAX_LENGTH spaces
> t = spaces.substring(0, s.length()); // this may be off by one, I'm
> never sure


Well, yes, but it's easy enough to test for exceeding the length of the
source string.

--
Paul Lutus
http://www.arachnoid.com

 
Reply With Quote
 
Steve Horsley
Guest
Posts: n/a
 
      10-17-2003
On Fri, 17 Oct 2003 20:16:22 +0100, VisionSet wrote:

>
> "Paul Lutus" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> This
>> is much faster:
>>
>> String original = "This is my test string.";
>> String replacement = original.replaceAll("."," ");
>>
>> System.out.println("[" + original + "]");
>> System.out.println("[" + replacement + "]");
>> System.out.println(original.length() - replecement.length());
>>
>> Result:
>>
>> [This is my test string.]
>> [ ]
>> 0
>>
>> Before the purists reply, remember -- the replaceAll() method is written

> in
>> native code, and "faster" was the criterion.
>>

>
> That's funny, I make your method 25x slower than my char[] one


That's strange. Because String is immutable, this:
str += " ";
has to be expanded to this:
str = newStringBuffer(str).append(" ").toString();
which creates two new objects every time round the loop.

Maybe the optimiser has figured out the redundant moves, moved a single
StringBuffer construction outside the loop (before) and a single
toString() outside the loop (after) and just left the append(" ") inside
the loop. That would explain the good performance, but it must be a pretty
clever optimiser. People do traditionally avoid string appends inside a
loop because of the poor preformance they usually get.

Steve.
 
Reply With Quote
 
Paul Lutus
Guest
Posts: n/a
 
      10-17-2003
Steve Horsley wrote:

< snip >

>> That's funny, I make your method 25x slower than my char[] one

>
> That's strange. Because String is immutable, this:
> str += " ";
> has to be expanded to this:
> str = newStringBuffer(str).append(" ").toString();
> which creates two new objects every time round the loop.


But that wasn't the approach he posted. He declared a char array of the
correct length, then filled it with spaces using a loop. It's entirely
reasonable.

--
Paul Lutus
http://www.arachnoid.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM
creating a webcontrol consisting of server controls tolisss ASP .Net 1 11-12-2005 06:14 AM
Compare 2 arrays consisting of hashes Frodo Larik Perl 1 05-30-2004 05:02 PM
sorting a LinkedList consisting of two items learningjava Java 6 12-15-2003 03:41 PM



Advertisments