Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String.Contains() - how about a literal string?

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
571
US
Colleagues,
Let's say you have a list of prefixes/IDs/whatever that are part of a file name, specifically - 1st two characters.
You go through the files' names in a dir and, depending on that 2-char prefix, rename this file.
Say, prefix may be "AB", "CD", "EF", "GH", but you're interested only in "CD" and "GH".
If it was up to me, I'd do the checking something like this:
Code:
Dim cStr As String, cFPref As String
cStr = "CD GH"
cFPref = Mid(cJustFileName, 1, 2)
If cStr.Contains(cFPref) Then
   cFileName ' etc.
However, I have no such luxury like adding my own code into the existing one (takes too long to explain the reasons).
I doubt that
Code:
String("CD GH").Contains(cFPref)
would work...
... or it would?
Please advise!
Tnx!

Regards,

Ilya
 
Hi Ilya,

I'd make use of "StartsWith" instead of Contains:
Code:
If cJustFileName.StartsWith("CD") OrElse cJustFileName.StartsWith("GH") Then
End If

This is assuming you got something like this:
Code:
cJustFileName = Path.GetFilename(cFile)
or
Code:
cJustFileName = Path.GetFilenameWithoutExtension(cFile)

Cheers,
MakeItSo

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top