Solved

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

Posted on 2014-04-16
7
297 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 48

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now