vb.net 如何在 Visual Basic 中遍历字母表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5434968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:22:50  来源:igfitidea点击:

How can I iterate through the alphabet in Visual Basic?

vb.net

提问by Mark Meuer

I need to generate a loop that would iterate through each capital letter of the alphabet using Visual Basic (2008). What's the cleanest way to do this?

我需要生成一个循环,该循环将使用 Visual Basic (2008) 遍历字母表的每个大写字母。什么是最干净的方法来做到这一点?

回答by manji

For Each c In "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
...
Next

回答by asleepysamurai

The simplest way to do this would probably be:

最简单的方法可能是:

For ascii = 65 To 90
    Debug.Print(Chr(ascii))
Next

I'm not really sure if it's the cleanest though. I haven't worked in vb.net much since I started with c#.

我不确定它是否是最干净的。自从我开始使用 c# 以来,我并没有在 vb.net 中工作过多少。

回答by Mark Meuer

I figured it out. Anyone have a better solution?

我想到了。有人有更好的解决方案吗?

To loop through the alphabet:

遍历字母表:

For i As Int16 = Convert.ToInt16("A"c) To Convert.ToInt16("Z"c)
        Dim letter As Char = Convert.ToChar(i)
        'Do my loop work...
Next

Letterwill first equal "A", then "B", etc.

Letter将首先等于“A”,然后是“B”,依此类推。

Anyone have a better solution?

有人有更好的解决方案吗?

回答by Meta-Knight

Here's another way to enumerate letters:

这是枚举字母的另一种方法:

For Each letter As Char In Enumerable.Range(Convert.ToInt16("A"c), 26) _
                                     .Select(Function(i) Convert.ToChar(i))
    Console.WriteLine(letter)
Next

回答by janmark

Try this one

试试这个

Dim a As String
a = 65
Do
If a > 90 Then
Exit Do
Else
List1.AddItem Chr(a)
a = a + 1
End If
Loop

回答by Martin Deveault

Sub Sel_1_sur_2()
Dim i As Long
Dim a As ChartObject
Dim rng As Range
Set rng = Range(Chr(65) & ":" & Chr(65))
For ascii = 65 To 90 Step 2
    Debug.Print (Chr(ascii))
    Set rng = Union(rng, Range(Chr(ascii) & ":" & Chr(ascii)))
Next
rng.Select
End Sub

回答by José Matos

Module Abecedario
    Sub main()
        Dim letra As String
        letra = 65

        Console.WriteLine("El abecedario")
        Console.WriteLine("-------------")

        Do
            If letra > 90 Then
                Exit Do
            Else
                Console.Write(Chr(letra) & ", ")
                letra = letra + 1
            End If
        Loop

        Console.ReadLine()

    End Sub
End Module