vb.net 如何将字符串 "one" 转换为整数 1

vb.net How to convert the string "one" to an integer 1

我认为我的问题很简单,但我似乎无法让它发挥作用。我必须将变量从字符串“One”更改为整数 1,如下所示

BeginningValue = Int32.Parse(numbers(index))

我也试过了

BeginningValue = Convert.ToInt32(numbers(index))

但这似乎也不起作用。这两行都给我错误“System.FormatException:输入字符串的格式不正确。”

没有将单词转换为数字的内置库。

如果您要转换的单词列表有限,那么您可以将它们存储在字典中并通过其键获取值。例如:

Dim wordsToNumber = New Dictionary(Of String, Integer)() From {
    { "One", 1 },
    { "Two", 2 },
    { "Three", 3 }
}

Dim input = "One"
If (wordsToNumber.ContainsKey(input)) Then
    Dim beginningValue = wordsToNumber(input)
    Console.WriteLine(beginningValue)
End If

但是,如果您想允许任何组合(例如一千二十五),那么最好使用现有算法。这是一个可以通过转换器放置的 C# 示例:https://www.c-sharpcorner.com/Blogs/convert-words-to-numbers-in-c-sharp

您可以为字符串创建扩展方法(代码来自 here)。

Module MyExtensions
<Extension()>
Function ParseWordsToNumber(ByVal number As String) As Integer
    Dim words As String() = number.ToLower().Split(New Char() {" "c, "-"c, ","c}, StringSplitOptions.RemoveEmptyEntries)
    Dim ones As String() = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
    Dim teens As String() = {"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
    Dim tens As String() = {"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}
    Dim modifiers As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)() From {
        {"billion", 1000000000},
        {"million", 1000000},
        {"thousand", 1000},
        {"hundred", 100}
    }
    If number = "eleventy billion" Then Return Integer.MaxValue
    Dim result As Integer = 0
    Dim currentResult As Integer = 0
    Dim lastModifier As Integer = 1

    For Each word As String In words

        If modifiers.ContainsKey(word) Then
            lastModifier = lastModifier * modifiers(word)
        Else
            Dim n As Integer

            If lastModifier > 1 Then
                result += currentResult * lastModifier
                lastModifier = 1
                currentResult = 0
            End If

            If Array.IndexOf(ones, word) + 1 > 0 Then
                n = Array.IndexOf(ones, word) + 1
                currentResult += n
            ElseIf Array.IndexOf(teens, word) + 1 > 0 Then
                n = Array.IndexOf(teens, word) + 1
                currentResult += n + 10
            ElseIf Array.IndexOf(tens, word) + 1 > 0 Then
                n = Array.IndexOf(tens, word) + 1
                currentResult += n * 10
            ElseIf word <> "and" Then
                Throw New ApplicationException("Unrecognized word: " & word)
            End If
        End If
    Next

    Return result + currentResult * lastModifier
End Function
End Module

那你就可以这样使用了:

BeginningValue = numbers(Index).ParseWordsToNumber

另一种选择是使用枚举。

Enum Numbers
    One = 1
    Two = 2
    Three = 3
End Enum

Sub foo()
    Dim input = "one"
    Dim number = [Enum].Parse(GetType(Numbers), input, True)
    Console.WriteLine($"input: {input}, number: {number}, value: {CInt(number)}")
End Sub

控制台:

input: one, number: One, value: 1

这在运行时不可扩展,例如 David 的字典,即字典可以从文件中获取其值,而枚举是使用有限数量的值编译的。但如果其他用例相关,您确实会获得智能感知。