Solved

I need to check the first 3 characters of a string in VB6

Posted on 2014-04-16
7
299 Views
Last Modified: 2014-05-11
Hi Experts,
I need to check the first 3 characters of a string in VB6.  I need to read a string and find out if the first 3 characters equal 'DF-'  .  How can I do this?

Thanks in advance,
mrotor
0
Comment
Question by:mainrotor
7 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 40005854
Hi,

pls try

If strYourString Like "DF-*" Then

Open in new window

Regards
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40005857
You need to read from:
- file
- db
- console
-
-
?

To check you use
if Left(yourString, 3) = "DF-" then
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 167 total points
ID: 40006040
Here's yet another way:
If InStr(MyString, "DF-") = 1 Then

Open in new window

If the case of the test strings is variable with some candidates starting "df-", "Df-" etc., you could use the expanded syntax:
If InStr(1, MyString, "DF-", vbTextCompare) = 1 Then

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 4

Assisted Solution

by:ravikantninave
ravikantninave earned 166 total points
ID: 40006102
Dim a, b, c As String
a = "DF-"
b = "DF-abcd"
c = "adjfldjfklsdf"

If Left(b, 3) = "DF-" Then MsgBox "b"
If Left(c, 3) = "DF-" Then MsgBox "c"

Hope it works.
0
 
LVL 1

Accepted Solution

by:
dwe0608 earned 167 total points
ID: 40028798
Hi

you want to test a string to see if the first 3 characters equals DF-

   Dim sResult As String
   dim StrToSearch as string
   Dim B As Integer

   StrToSearch = "DF-1234567890"

   sResult = Mid$(StrToSearch, 1, 3)
   B = StrComp(sResult, "DF-", vbTextCompare)
           
If B = 0 Then the string has been found - otherwise it has not.

You could if you wished eliminate the necessity for sResult like this:

 B = StrComp(Mid$(StrToSearch, 1, 3), "DF-", vbTextCompare)

And you could encapsulate the whole thing into a function as follows:

Function SearchStr(strToSearch As String, sFind As String, iStart As Integer, iLen As Integer) As Boolean
 Dim b%
 b = StrComp(Mid$(strToSearch, iStart, iLen), sFind, vbTextCompare)
 SearchStr = IIf(b = 0, True, False)
End Function

Open in new window


You would call the function like this:

if strtosearch(strtosearch, "DF-",1 ,3) then
    ' found it
else
   ' not found
end if

Open in new window


HTH

DWE
0
 
LVL 1

Expert Comment

by:dwe0608
ID: 40028895
I've tightened up the function above a little more as well ...

Function SearchStr(strToSearch As String, sFind As String, iStart As Integer, iLen As Integer) As Boolean
 SearchStr = IIf(StrComp(Mid$(strToSearch, iStart, iLen), sFind, vbTextCompare) = 0, True, False)
End Function

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This is about my first experience with programming Arduino.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

770 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