Solved

test for additional space

Posted on 2011-03-08
8
322 Views
Last Modified: 2012-06-27
I would like to test two strings to see if a space has been added.

str1 "100S LWB CH5S"

str2 "100 S LWB CH5S"

The string is essentially the same text but only had a space added at some part.

Is that possible to test for

ie determine if space added and if added add the space temporarily to str1 and then do a compare to make sure they are equal strings?

0
Comment
Question by:PeterBaileyUk
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 75
ID: 35079209
Well, you can use StrComp to see if the are equal or note:

example:

strcomp("100S LWB CH5S","100 S LWB CH5S")

returns 1 because they are not equal

StrComp returns :
string1 is less than string2 -1
string1 is equal to string2 0
string1 is greater than string2 1
string1 or string2 is Null Null

mx

0
 

Author Comment

by:PeterBaileyUk
ID: 35079750
and is it easy to find where the space belongs in field 1?

i am thinking maybe checking each character until they dont match and at that point string2(position string1 +1) should be a space and if it is then add a space at string1(position)

0
 

Author Comment

by:PeterBaileyUk
ID: 35079770
or another thought find current space positions in string2

string 1 positions should correspond to string 2 bar 1 and thats my insertion point on string 1

0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 75
ID: 35079809
"or another thought find current space positions in string2"
You can use the InStr() function to find the first occurrence of a Space (or any character)

InStr(1,SearchString, SearchingFor) returns the first position fo Searching For

InStr(1,"100 S LWB CH5S", " ") returns 4

mx
0
 
LVL 3

Accepted Solution

by:
JAMcDo earned 500 total points
ID: 35079988
You may wish to try this function:

Function MakeEqualStr(Str1 As String, Str2 As String) As String

    Dim Pntr As Integer
    Dim MaxPntr As Integer
   
    Pntr = 1
       
    MaxPntr = IIf(Len(Str1) < Len(Str2), Len(Str1), Len(Str2))
   
    Do While StrComp(Str1, Str2) <> 0
        Select Case StrComp(Left$(Str1, Pntr), Left$(Str2, Pntr))
            Case -1
                Str2 = Left$(Str1, Pntr) & Mid$(Str2, Pntr)
            Case 1
                Str1 = Left$(Str2, Pntr) & Mid$(Str1, Pntr)
            Case 0
                Pntr = Pntr + 1
                If Pntr > MaxPntr Then Exit Do
        End Select
    Loop
    MakeEqualStr = Str1
End Function

Create -> Module and copy/paste the function into a module.

J.
0
 
LVL 77

Expert Comment

by:peter57r
ID: 35080344
I would use Trim() in your compare.  That would get rid of unnecessary spaces.

If trim(fielda) = Trim(fieldb) then...
0
 

Author Closing Comment

by:PeterBaileyUk
ID: 35081078
worked great thx
0
 
LVL 3

Expert Comment

by:JAMcDo
ID: 35081435
Thanks for the points,

J.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question