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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is a Space? 2

Status
Not open for further replies.

rvBasic

Programmer
Oct 22, 2000
414
BE
I recently got an irregular text file to format for further use. Part of the program I wrote tried to eliminate spaces. I used Regular Expressions to perform the job (I know, I could have used the Replace function, but the problem would have been the same)

Here's the coding excerpt:
Dim strTempNames as String
Dim objRegE As RegExp
Set objRegE = New RegExp
objRegE.Pattern = "\s"
objRegE.Global = True 'Replace them all by nothing
strTempNames = objRegE.Replace(strTempNames, vbNullString)


To my surprise, the resulting string still contained "spaces". After some investigation and dumping the memory string, I discovered that the original string contained characters represented by Hexadecimal(A0) or Char(160). The latter is a valid ANSI space character (cfr: VB6 language reference manual, ANSI character set p1148)

Therefore to be sure to eliminate spaces, use the following code fragment:

Const c_strSpaces as string = "\x20|\xA0"
Dim strTempNames as String
Dim objRegE As RegExp
Set objRegE = New RegExp
objRegE.Pattern = c_strSpaces
objRegE.Global = True 'Replace them all by nothing
strTempNames = objRegE.Replace(strTempNames, vbNullString)


PS: Be sure to set a reference to RegExp in the Project

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
You can copy it to a file, then apply "Trim" "LTrim" or
"RTrim' to it. They all are designed to delete any spaces.
 
Thomas, the Trim functions only remove spaces that are at the beginning or end, or both sides, of a string, not spaces in the middle of a string. So they do not delete "any" spaces.

Robert
 
Syntax for
Replace(expression, find, replacewith[, start[, count[, compare]]])
The Replace function syntax has these parts:
Part Description
expression Required.String expression containing substring to replace.
find Required. Substring being searched for.

You missed secong argument - find
Try this way, it has to help
Code:
strTempNames = objRegE.Replace(strTempNames," ", vbNullString)
 
kel1981b, be aware that rvBasic is using Regular Expressions, not the Replace function.

THOMASNG, whilst I'm sure we all appreciate your efforts to offer some advice, once again you appear to have answered a question without seeming to have read it completely. Firstly, as TheVampire has already pointed out, the Trim functions only work on leading and trailing spaces and so would not be of benefit in the scenario that rvBasic has indicated; secondly, the Trim functions only work on regular spaces (ASCII 32), not on the non-breaking spaces represented by ASCII 160, so again their use would not help rvBasic.
 
strongm
Did you read his message? He said '... (I know, I could have used the Replace function, but the problem would have been the same)'

No matter what he used his problem is second argument

IT MUST BE STRING not VB constant and in his case it must be = SPACE not NULL. As you know SPACE and NULL are two very different animals.
He needs something like that
Code:
Dim strTest as string
if strTest = " "
  strTempNames = objRegE.Replace(strTempNames, strTest)
end if
or
Code:
strTempNames = objRegE.Replace(strTempNames," ")
vbNullString is not = SPACE it's NULL





 
kel1981b

I believe that the second parameter in RegExp replace function is the replacement string, in this case to be an empty string.

VbNullstring is an intrinsic constant, giving a string having value 0. A better choice in this case would have been just an empty string ""

VBHElp says <vbNullString String having value 0 Not the same as a zero-length string (&quot;&quot;); used for calling external procedures. >
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
johnwm

He wants to replace SPACE not empty string or NULL. SPACE is ASCII character. It's NOT NULL and NOT EMPTY string!!!
 
I obviously didn't make myself clear. When I said:

I believe that the second parameter in RegExp replace function is the replacement string, in this case to be an empty string.

the replacement string is the new string you are using to replace the old string.

In the simple VB replace function, there are 3 parameters, and the second parameter is the old string to be replaced.

In RegExp Replace which is what we're talking about there are 2 parameters and the second is the replacing (new) string, not the replaced (old) string.
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
What about other Non-Display characters, characters not between Char(33) and Char(126) that may show up from time to time.
 
As johmwm makes clear, vbNullString is absolutely fine as the second parameter in a Regular Expression Replace method. The string pattern being replaced is, strangely enough, defined in the RegExp's Pattern property.

Did I read his message? Yes, somewhat more carefully than some it would appear. Do I know what I am talking about when it comes to Regular Expressions? Yes, I have a little bit of experience...
 
I never imagined such an avalanche of replies. strongm thanks for your comments to the point.

When I wrote &quot; I could have used the Replace function, but the problem would have been the same&quot; I meant that using
Replace(strTempNames,&quot; &quot;,vbNullString) would not have eliminated the non-regular chr(160) space from my string. On top I would have to use the Replace function twice, since the second argument cannot take the form strFind1 OR strFind2

DrJavaJoe to eliminate non-display characters not between char(33)-char(126) use the pattern as shown in the following excerpt:
Const c_strAllWhiteSpaces As String = &quot;[\x00-\x20|xA0]&quot;

Dim strTempNames As String

Dim objRegE As RegExp
Set objRegE = New RegExp

strTempNames = &quot; non white&quot; & Chr(0) & Chr(160) & Chr(31) & &quot;spaces&quot;
Debug.Print strTempNames

objRegE.Pattern = c_strSpaces
objRegE.Global = True 'Replace them all by nothing
strTempNames = objRegE.Replace(strTempNames, vbNullString)
Debug.Print strTempNames


If you only want to eliminate spaces and printing control characters (tab,newline,carriage return,formfeed and vertical tab) use the following pattern string:
Const c_strWhitePrintSpaces As String = &quot;\s|\xA0&quot;

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Of course , the code should read as follows:
Const c_strAllWhiteSpaces As String = &quot;[\x00-\x20|xA0]&quot;

Dim strTempNames As String

Dim objRegE As RegExp
Set objRegE = New RegExp

strTempNames = &quot; non white&quot; & Chr(0) & Chr(160) & Chr(31) & &quot;spaces&quot;
Debug.Print strTempNames

objRegE.Pattern =
c_strAllWhiteSpaces
objRegE.Global = True 'Replace them all by nothing
strTempNames = objRegE.Replace(strTempNames, vbNullString)
Debug.Print strTempNames


_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Thanks rvBasic I've been cycling thru each byte and if it's not between replacing it with a &quot; &quot;. This should be much more efficient. Thanks to strongm too for turning me on to RegExp, I've never used them in the past. Stars for both of you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top