Solved

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

Posted on 2014-04-16
7
300 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 50

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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 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 …

820 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