Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi all. Does anyone know of a VBSc

Status
Not open for further replies.

Kenny100

Technical User
Feb 6, 2001
72
NZ
Hi all. Does anyone know of a VBScript function that can remove extraneous spaces from the middle of a string?

Ex. If I read in the text 'hello world' to a variable strText, is there someway I can remove the space easily given that I won't always know the length of the string?

Cheers for any help. Kenny.
 
Say I had a string, "How now brown cow", and it was in a variable, myString
Code:
myString = "How now brown cow"
I could then use the split function to put it into an array:
Code:
dim myArray(10)
myArray = split(myString)
Now, I have a ten element array that contains the four different words. You would want to make sure that the array had enough elements to hold all the words.

Then, you can just loop through concatenating the strings as you go:
Code:
for i = 1 to 10
  newString = newString & myArray(i)
next

The following code:
Code:
response.write(newString)

would produce the following output:
Code:
Hownowbrowncow

There's probably a more efficient way to do it, but that's my two cents.

Good luck!:)
Paul Prewett
 
I think replace should work.

dim temp
temp = "hello world"
temp = replace(temp, " ", "")

temp should then be helloworld.

HTH,
Earme
 
What earme says looks indeed like the shortest version, but commenting on the loop to concatenate the strings, instead of:
Code:
for i = 1 to 10
  newString = newString & myArray(i)
next
use:

Code:
newString = Join(myArray(i))

or join with a delimiter, like

Code:
newString = Join(myArray(i), " ")

(this will give you exactly one space between all the words.)
 
I *think* that you aren't supposed to put in the (i) for the join function --

Just plain ole
Code:
join(myArray)

And yes, much more efficient that my first suggestion:)

But even this is less efficient than earme's suggestion.

Paul Prewett
 
Earme's suggestion is indeed the shortest and quickest. But if you want to remove all extraneus spaces (and not all spaces), you may as well use the split/join structure.

BTW, you are completely right about not including the (i) part. Yours,

Rob.
 
Join and split have a lot of allocating and reallocating dynamic array overhead. The most efficient is really:

last_char = left(mystring, 1)
new_string = last_char
for i = 2 to len(myString)
cur_char = mid(mystring, i, 1)
if last_char = " " then
if cur_char <> &quot; &quot; then
new_string = new_string & cur_char
last_char = cur_char
end if
else
new_string = new_string & cur_char
last_char = cur_char
end if
next

 
That may seem the case, but look at this (from

----------
Tip 20: Avoid String Concatenation in Loops
Many people build a string in a loop like this:


Code:
s = &quot;<table>&quot; & vbCrLf
For Each fld in rs.Fields
    s = s & &quot; <th>&quot; & fld.Name & &quot;</th> &quot;
Next

While Not rs.EOF
    s = s & vbCrLf & &quot; <tr>&quot;
    For Each fld in rs.Fields
        s = s & &quot; <td>&quot; & fld.Value & &quot;</td> &quot;
    Next
    s = s & &quot; </tr>&quot;
    rs.MoveNext
Wend

s = s & vbCrLf & &quot;</table>&quot; & vbCrLf
Response.Write s

There are several problems with this approach. The first is that repeatedly concatenating a string takes quadratic time; less formally, the time that it takes to run this loop is proportional to the square of the number of records times the number of fields. A simpler example should make this clearer.

Code:
s = &quot;&quot;
For i = Asc(&quot;A&quot;) to Asc(&quot;Z&quot;)
    s = s & Chr(i)
Next

On the first iteration, you get a one-character string, &quot;A&quot;. On the second iteration, VBScript has to reallocate the string and copy two characters (&quot;AB&quot;) into s. On the third iteration, it has to reallocate s again and copy three characters into s. On the Nth (26th) iteration, it has to reallocate and copy N characters into s. That’s a total of 1+2+3+...+N which is N*(N+1)/2 copies.

In the recordset example above, if there were 100 records and 5 fields, the inner loop would be executed 100*5 = 500 times and the time taken to do all the copying and reallocation would be proportional to 500*500 = 250,000. That’s a lot of copying for a modest-sized recordset.

In this example, the code could be improved by replacing the string concatenation with Response.Write() or inline script (<% = fld.Value %>). If response buffering is turned on (as it should be), this will be fast, as Response.Write just appends the data to the end of the response buffer. No reallocation is involved and it’s very efficient.

In the particular case of transforming an ADO recordset into an HTML table, consider using GetRows or GetString.

If you concatenate strings in JScript, it is highly recommended that you use the += operator; that is, use s += &quot;some string&quot;, not s = s + &quot;some string&quot;.


---------

For larger strings, the string concatenation in your code may even result in more re-allocs than you expect. Furthermore, Split() and Join() are compiled and probably optimised routines. I'd have to do a performance benchmark on them. Yours,

Rob.
 
I'm familiar with the overhead involved in concat'ing strings.. In VB you could use a byte arrays, which would be very fast and eliminate the lefts and mids that are the slowest part of this code (not the string concat).

I also neglected to take into consideration that the intrinsic functions are of course compiled and faster, though this shouldn't be as much of a problem in ASP.Net

As long as the strings are dim'ed in the function, the concat will be more memory efficient, but it will be slower.

Anyway, I benchmarked the results using softwing.Profiler:
In IIS 5 (256MB RAM/PIII 733/2000 Pro SP1):
Time to split and join: 4.52012755811144
Time to use string concat: 8.54019156065925
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top