Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-04-16
7
Medium Priority
?
306 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
[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
7 Comments
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 40005854
Hi,

pls try

If strYourString Like "DF-*" Then

Open in new window

Regards
0
 
LVL 35

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 668 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 4

Assisted Solution

by:ravikantninave
ravikantninave earned 664 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 668 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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 …
Suggested Courses

722 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