VampireMonkey
VampireMonkey

Reputation: 177

Iterate Through Unicode Ranges

I want to get the range of characters between any two Unicode points.

This below doesn't work in VB.NET and doesn't give any hints on how to convert it to use Unicode instead.

for (char c = 'A'; c <= 'Z'; c++)
{
    yield return c;
} 

The above is from here

Converted it looks like this :

For c As Char = "A"c To "Z"c
    Yield c
Next

But this causes an error "'For' loop control variable cannot be of type 'Char' because the type does not support the required operators."

Say I want to get Basic Latin, is there any way to do it like below?

For c As Char = U+0000 To U+007F
    Yield c
Next

I'm struggling to find anything on this specific subject.

Upvotes: 1

Views: 358

Answers (2)

41686d6564
41686d6564

Reputation: 19651

Another way to do it is by using Enumerable.Range. Here's a one-liner:

Enumerable.Range(AscW("A"c), AscW("Z"c) - AscW("A"c) + 1).Select(Function(i) ChrW(i))

You can put it in a function like this:

Public Function CharRange(first As Char, last As Char) As IEnumerable(Of Char)
    Return Enumerable.Range(AscW(first), AscW(last) - AscW(first) + 1).Select(Function(i) ChrW(i))
End Function

For the Unicode version, you'd need to use Integers instead of Chars:

Public Function CharRange(first As Integer, last As Integer) As IEnumerable(Of Char)
    Return Enumerable.Range(first, last - first + 1).Select(Function(i) ChrW(i))
End Function

Also, now that we have an Integer version, you can replace the first function with this shorter version:

Public Function CharRange(first As Char, last As Char) As IEnumerable(Of Char)
    Return CharRange(AscW(first), AscW(last))
End Function

Sample usage:

'Dim combined As String = String.Join("", CharRange("A"c, "Z"c))
Dim combined As String = String.Join("", CharRange(&H41, &H5A))
Console.WriteLine(combined)

Output:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Upvotes: 2

Visual Vincent
Visual Vincent

Reputation: 18310

VB.NET unfortunately does not treat chars the same way C# do. A char is actually just a number (known as a character code) that represents letter, so to a computer it actually makes sense that you would be able to use them in a loop.

However, for it to work in VB.NET you would have to convert the chars into integers first to be able to use them in a loop, then convert the integer in each iteration back into a Char:

For i As Integer = AscW("A"c) To AscW("Z"c)
    Dim c As Char = ChrW(i)
    Yield c
Next

As for your second example, Unicode code points are represented in the form U+####. The #### part is a hexadecimal number, which can be written in VB.NET in the form &H####. To the compiler a hexadecimal number is just a normal number, so all you need to do is to change your loop to:

For i As Integer = &H0000 To &H007F
    Dim c As Char = ChrW(i)
    Yield c
Next

Upvotes: 1

Related Questions