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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Instr function can't find a character 1

Status
Not open for further replies.

sglab

Technical User
Jul 29, 2003
104
US
Hello everyone,

I have this weird - at least to me - problem. I have files' names that contain "/" characters as in [blue]"10?13?2010 RER Weekly eNewsletter [72A6][122096].eml.msg.htm"[/blue]. The task is to replace "?" for "_". When I go character-by-charater in the file name, using Mid function as in Mid(fName,i,1) then "?" is recognized as character 47. But when I use Instr(1, fName, chr(47)) it returns 0.
I hope you can see the difference between character "?" and "/" . How come both have ASC code of 47. What is that character "?" ?

Thank you in advance.

 
this doesn't answer your question but if the "split" command is available in VB6, you might want to check it out. It will parse by whatever character you choose....HTH

Ernest

Be Alert, America needs more lerts
 
Hello Ernest,

thank you for your suggestion. I have tried using Split - it does exist in VB - and it returned 0 as Ubound of array it was supposed to create. Did you mean some other Split.
Have you tried doing something with the string I pasted in my initial post? Did it work for you?

Thank you,

Sergey.
 
Why do you need to go character by character?


replace(string,"/","_") will replace them all in the string at one call.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Sorry for not answering sooner - was working on a rush project.

Thank you all for your replies.

Chris, this is the reason I went here with my question. Neither Instr nor Replace can pick up this character "?". It's not "/". When I go by character, I get its ASCII code as 47, but when I use INSTR oR REPLACE they can't find it.

Thank you
 
I did not actually try the split command. I have used it in the past with success. You say that the "/" shows as a chr(47) but when you use chr(47) it doesn't work. what are you using to determine that the / is a chr(47)?

Ernest

Be Alert, America needs more lerts
 
judgeh59,

I used something like below:

for i = 1 to len(fName)
debug.print Asc(Mid(fName,i,1))
next

So, at position numbers 3 and 6 it would print "47" (see the string in my initial post)

At the same time Instr(1,fName,chr(47)) returns 0

Anyway, thanks to one of the other Forum's members, who suggested I'd use regular expression to accomplish what I needed, I was able to solve my problem.

Thanks to everyone for all their help.

Best regards.
 
I'm not sure how VFred's Split helps here, so the link above is more than a little strange. I think it's clear strongm already told you enough to solve your problem.


That's the U+2215 "Division slash" most easily represented in VB6 source code as [tt]ChrW$(8725)[/tt].

To handle it properly you need to avoid ANSI conversions, which may change it to either an ANSI "/" or a "?" depending on the context.
 
Instr requires an integer and 2 strings. By using chr(47), you are giving it chr, you are giving it an integer, a string and a char. VB does not translate a char to a string. You may need something silly like cstr(chr(47)) to convert it to a string.
 
Hello dilettante (?),

thank you for your input, too. I mean, this whole conversation in this topic has been a valuable lesson for me.
So, you're saying that conversion to ANSI occurs when one uses Asc to determine the code for the character, right? Instead, one should use AscW, right?

Best regards.
 
xwb,

thank you for pointing that out - duly noted - but even with CStr(Chr(n)) Instr wouldn't return a position of the character I was looking for.

Best regards.
 
Yes, Asc(x) means "Convert the first character of x from Unicode to ANSI using the current codepage, then return its 8-bit value as an Integer."

AscW() does not convert and returns a Long value.
 
I just tried the following in VS2008. Seems to give the same answers. I'll have to try it in VB6.

Code:
Module instrtest

   Sub Main()
      Dim slashstr As String = "10/20/30 dkdk"
      Dim posn As Integer

      posn = InStr(slashstr, "/")
      Console.WriteLine("With a / string {0}", posn)

      posn = InStr(slashstr, Chr(47))
      Console.WriteLine("With a chr string {0}", posn)

   End Sub

End Module
 
The problem is rather complicated.

VB6 code editor does not support Unicode characters. So when you copy-paste a string or file name containing "?" like "10?13?2010 RER Weekly eNewsletter [72A6][122096].eml.msg.htm" to VB6 code window, VB6 requests ANSI version of the string from clipboard and OS automatically converts "?" to "/". All VB6 code modules are plain ANSI text files and they are unable to store or retain unicode characters.

Similarly, if the file "10?13?2010 RER Weekly eNewsletter [72A6][122096].eml.msg.htm" resides somewhere on disk and you try to retrieve the file name using Dir function or FileSystemObject or CommonDialog control, all these methods return the file name with "?" replaced with "/" automatically, and the returned file name will never represent the file in question.

There is only one way to get around this. You have to use Win32 API functions. And most API functions which manipulate strings come in two versions, ANSI and Wide. You have to use Wide versions which support Unicode. This is contrary to the fact that most of the time we use ANSI API functions in VB6.

An example is MoveFile function which is used to move or rename a file. It has two versions, MoveFileA (ANSI) and MoveFileW (Wide). MoveFileA function cannot handle file names with Unicode characters.

Note that using Unicode functions in VB6 is not easy because VB6 has limited support for Unicode and string manipulation becomes very difficult when working with Unicode strings in VB6. You have to rely on API functions for carrying out even trivial tasks, like displaying a message box, because VB's MsgBox function does not support Unicode.
 
While not ideal, the FSO handles Unicode in file names and Unicode text files with no problems. There are also 3rd party Unicode-capable I/O libraries, the ADO Stream object, and other ways to do Unicode I/O without resorting to API calls.

Furthermore, almost all of the string functions in VB6 are fully Unicode enabled. The only real exceptions are Chr$() and Asc() and of course the special B-suffix functions like MidB$().

The main reason most native "I/O" operations in VB6 convert to/from ANSI has to do with Win9x and Internet support. Most Internet protocols are still fundamentally based on 8-bit character sets.

Considering how rare a Unicode I/O requirement really is, this isn't much of a handicap. It can get in the way at times of course and String literals are one of the more annoying cases.


Many people aren't even aware that VB6 got a new Unicode control starting with XP Tablet Edition: InkEdit. This is a fully-Unicode replacement for TextBox and RichTextBox that also accepts "ink" (handwriting recognition) input. It was made generally available in Vista and is present in Windows 7 and the Windows 8 Dev Preview. There is even a non-ink version that can be deployed downlevel to XP and Win2K.
 
>While not ideal, the FSO handles Unicode in file names and Unicode text files with no problems.

Did not know about this. Tested it myself and it was nice to see FSO doing good with Unicode.

VB's internal controls (textbox, label, listbox etc.) are not unicode enabled. While InkEdit is capable of doing so as mentioned above (did not know that either), another good alternate is Microsoft Forms 2.0 Object Library controls (fm20.dll) which has unicode enabled standard controls like textbox, command button, label etc.

Thus you can retrieve the file list in a directory having unicode names using FSO and show them correctly in a listbox. This is not possible with VB6's built in ListBox or even FileList control.

Microsoft Forms 2.0 Object Library is installed with Microsoft Office and is not distributed as a standalone library.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top