Solved

test for additional space

Posted on 2011-03-08
8
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

728 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