Hi there,
I’m trying to test if a filename is what I think it is. The filename is Franais.txt. However, when I test f.name against the string “Franais.txt”, they don’t match. I’m guessing it’s the cedilla, but I’m blowed if I can work out why.
This code demonstrates:
Dim count As Integer = SpecialFolder.Desktop.Count
For i As Integer = 1 to count
Dim f As FolderItem = SpecialFolder.Desktop.Item(i)
If f <> Nil Then
if left(f.Name, 4)="Fran" then
if f.Name = "Franais.txt" then
break
else
break
end if
end if
End If
Next
To reproduce this, make a text file on the desktop whose name is Franais.txt - you’ll see it breaks on the second break, whereas I’d expect it to break on the first.
I’ve tried doing a ConvertEncoding on both to UTF-8, but that makes no difference.
[quote=219072:@Greg O’Lone]if you were to look at the hexadecimal representation of those two strings in the debugger, you’d see that the one coming from f.name looks like this:
4672 616E 63CC A761 6973 2E74 7874
Whereas the one from the string literal looks like this:
I can reproduce too. This works however, as expected:
dim target as text = "Franais.txt"
Dim count As Integer = SpecialFolder.Desktop.Count
For i As Integer = 1 to count
Dim f As FolderItem = SpecialFolder.Desktop.Item(i)
If f <> Nil Then
if left(f.Name, 4)="Fran" then
dim nameAsText as text = f.Name.ToText
if nameAsText = target then
break
else
break
end if
end if
End If
Next
This is a perfect illustration as to why Text is a better conceptual representation of human-readable text. It represents the actual series of characters rather than the encoded bytes that represent those characters.
Is this maybe an OS thing? Because here on OS X the code of the original post works (provided there is no other file having a name starting with “Fran”).
This is a good occasion to use Text instead of String. Text is able to compare graphemes, so it does not mind the number of bytes :
Dim f as FolderItem = SpecialFolder.Desktop.child("a?e?e?c?a?u?a?e?i?o?u?.txt")
dim filename as Text = f.name.ToText
dim compareto as text = "a?e?e?c?a?u?a?e?i?o?u?.txt"
if filename = compareto then msgbox "yeah !"
I’m a bit puzzled. Using string should not work on the Mac then? Because I get equal on my Mac:
Dim f as FolderItem = SpecialFolder.Desktop.Child(".txt")
dim Filename as String = f.Name
dim CompareTo as String = ".txt"
if Filename = CompareTo then
MsgBox "Equal"
else
MsgBox "Not equal"
end if
[quote=219144:@Markus Winter]I’m a bit puzzled. Using string should not work on the Mac then? Because I get equal on my Mac:
Dim f as FolderItem = SpecialFolder.Desktop.Child("äéèçàùâêîôû.txt")
dim Filename as String = f.Name
dim CompareTo as String = "äéèçàùâêîôû.txt"
if Filename = CompareTo then
MsgBox "Equal"
else
MsgBox "Not equal"
end if
So it works for some and not for others???[/quote]
At the time I did verify quite clearly the issue with String. I have no idea why it varies from system to system. However, I am positive Text is the best way to avoid possible issues. If it does nothing, it does not hurt. Whereas String seems unreliable enough not to risk issues with customers, IMO.
new folder - type in name
the code doesnt care what kind of file it is
the op’s code fails here as well but markus code works
gawd i love composed vs decomposed utf-16 vs utf-8 etc
I just tried the OP code on both Mac and PC, the strings are equal, if I type from my French keyboard or from the Emoji and Symbols palette. I suspect the name of the file may have been created by copy/paste from another source which uses precomposed.
(keyboard) looks identical to c + &u0327 (c plus non advancing cedilla). This does not find equal :
Dim count As Integer = SpecialFolder.Desktop.Count
For i As Integer = 1 to count
Dim f As FolderItem = SpecialFolder.Desktop.Item(i)
If f <> Nil Then
if left(f.Name, 4)="Fran" then
dim nom as string = "Franc"+&u0327+"ais.txt"
system.debuglog nom
if f.Name = nom then
break
else
break
end if
end if
End If
Next
The most annoying is that even using text, it does not find equal…