How to Replace("String Irregular # of space", "?", "") 
Author Message
 How to Replace("String Irregular # of space", "?", "")

I have a string with irregular # of space characters.
Such as:

string = "              lastname,  firstname"
string = " lastname, firstname "
string = "   lastname, firstname "

I want to get "lastname, firstname".

What's the best way to get that done in this case ? I couldn't handle that
with Replace.



Sun, 05 Jun 2005 23:06:43 GMT  
 How to Replace("String Irregular # of space", "?", "")
Did you try doing something like

string = Trim(string)

Which would get rid of any spaces at the beginning, or any at the end.

But I guess that would not fix a case like this:

string = "lastname,            firstname"

Where you had multiple spaces b/t the last and first name.

So you could do this:

string = replace(string," ","")
string = replace(string,",",", ")

Which would replace ALL spaces, even the one after the comma

But then you could replace the comma with the ", " (comma then space)

Hope that is what you are looking for.

Thanks
Matt


Quote:
> I have a string with irregular # of space characters.
> Such as:

> string = "              lastname,  firstname"
> string = " lastname, firstname "
> string = "   lastname, firstname "

> I want to get "lastname, firstname".

> What's the best way to get that done in this case ? I couldn't handle that
> with Replace.



Sun, 05 Jun 2005 23:25:30 GMT  
 How to Replace("String Irregular # of space", "?", "")

Quote:
> I have a string with irregular # of space characters.
> Such as:

> string = "              lastname,  firstname"
> string = " lastname, firstname "
> string = "   lastname, firstname "

> I want to get "lastname, firstname".

> What's the best way to get that done in this case ? I couldn't handle that
> with Replace.

Marlon,
this is best done with two steps.  First trim the string; then keep checking the
string for "  " (two spaces) and replace with one until done.  Here's an
example:

s = Trim(s)
Do While InStr(s, "  ")
 s = Replace(s, "  ", " ")
Loop
WScript.Echo s

--
Please respond in the newsgroup so everyone may benefit.
 http://dev.remotenetworktechnology.com
 ----------
 Subscribe to Microsoft's Security Bulletins:
 http://www.microsoft.com/technet/security/bulletin/notify.asp



Sun, 05 Jun 2005 23:36:14 GMT  
 How to Replace("String Irregular # of space", "?", "")

Quote:

> I have a string with irregular # of space characters.
> Such as:

> string = "              lastname,  firstname"
> string = " lastname, firstname "
> string = "   lastname, firstname "

> I want to get "lastname, firstname".

> What's the best way to get that done in this case ? I couldn't handle that
> with Replace.

strs = Array(_
  "              lastname,  firstname", _
  " lastname, firstname ", _
  "   lastname, firstname "_
)

WScript.echo vbNewLine & "Method #1: Trim + simple RegExp"
For Each str In strs
  With New RegExp
    .Pattern = "\s{2,}"
    .Global = True
    str = .Replace(Trim(str), " ")
    WScript.echo """" & str & """"
  End With
Next

WScript.echo vbNewLine & "Method #2: More complex RegExp"
For Each str In strs
  With New RegExp
    .Pattern = "^\s+|\s+$|\s+(?=\s)"
    .Global = True
    str = .Replace(str, "")
    WScript.echo """" & str & """"
  End With
Next

LTrim; RTrim; and Trim Functions
http://msdn.microsoft.com/library/en-us/script56/html/vsfctltrim.asp

Introduction to Regular Expressions
http://msdn.microsoft.com/library/en-us/script56/html/reconintroducti...

Windows Script Documentation
http://msdn.microsoft.com/downloads/sample.asp?url=/MSDN-FILES/027/00...

--
A life spent making mistakes is not only more honorable but more useful than a
life spent doing nothing. -George Bernard Shaw

=-=-=
Steve
-=-=-



Sun, 05 Jun 2005 23:52:48 GMT  
 How to Replace("String Irregular # of space", "?", "")

Quote:

> I have a string with irregular # of space characters.
> Such as:

> string = "              lastname,  firstname"
> string = " lastname, firstname "
> string = "   lastname, firstname "

> I want to get "lastname, firstname".

> What's the best way to get that done in this case ? I couldn't handle that
> with Replace.

Hi

sString = "   lastname,   firstname   "
aName = Split(sString, ",")
sString = Trim(aName(0)) & ", " & Trim(aName(1))

--
torgeir
Microsoft MVP Scripting and WMI
Porsgrunn Norway



Mon, 06 Jun 2005 01:55:49 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Replace Blank Space With "+"

2. Disabling "BACK"/"FORWARD" buttons

3. CreateObject("Excel","//server"), MsgBox output

4. Problem With "window.showmodaldialog("")"

5. Disabling "BACK"/"FORWARD" buttons

6. *"*-.,._,.-*"* I"LL TRADE VISUAL C++ FOR VBASIC *"*-.,_,.-*"*

7. JavaScript "\"" and WShell Run

8. "extra space" appearing within words

9. sendkeys and "{space}"

10. WshShell.Run "path with spaces" fails

11. ~ How can I Change "string" to "STRING" ~

12. question for "location.replace"

 

 
Powered by phpBB® Forum Software