【问题标题】:Having trouble splitting a PascalCase string and storing it in an array in VB在 VB 中拆分 PascalCase 字符串并将其存储在数组中时遇到问题
【发布时间】:2017-05-14 23:53:26
【问题描述】:
CorrectHorseBatteryStaple

Correct
Horse
Battery
Staple
(Empty)

还有一个问题,我不能使用除了 Mid()、Right()、Left()、Len()、Asc() 之外的类、函数、内置函数。这让整个事情变得更加困难。

我这辈子都不知道如何比较字符串中的字符并以某种方式停止循环/将第一个单词存储在数组中等等。

这是我到目前为止所做的,没有任何意义:

Sub Main()
    Dim input As String
    Dim str(5) As String
    Dim tempstr As String
    Dim temp As Char
    Dim temp2 As Char
    Dim l As Integer
    Console.WriteLine("Enter the string: ")
    input = Console.ReadLine()
    l = Len(input)
    For z As Integer = 1 To 5
        For i As Integer = 1 To l
            temp = Mid(input, i, l)
            temp2 = Mid(input, i + 1, l)
            If Asc(temp) > 65 And Asc(temp) < 90 Then
                tempstr = temp
                If Asc(temp2) > 65 And Asc(temp2) < 90 Then
                    tempstr = temp
                Else
                    tempstr = tempstr & temp
                End If
            Else
                tempstr = tempstr & temp
            End If
        Next i
        str(z) = tempstr
    Next z
    For a As Integer = 1 To 5
        Console.WriteLine(str(a))
    Next a
    Console.ReadKey()
End Sub

【问题讨论】:

    标签: arrays vb.net pascalcasing


    【解决方案1】:

    在开始之前,我建议您使用列表而不是数组。这样,如果您想拆分更多单词,则无需更改代码。但是,我猜您还没有涵盖这些内容。所以...

    最简单的方法是循环遍历数组的每个字符,如果字符是大写的,则移动到下一个数组项并将该字符添加到数组项中。如果字符是小写,则只需将其添加到当前数组项。这样就不需要使用这么多变量了。

    这里假设第一个字母是大写的。如果没有,就会有一个

    索引超出范围

    错误。


    给你..

    Module module1
        Sub Main()
    
            Dim input As String
            Dim str(3) As String
            Dim temp As String
            Dim l As Integer
            Dim z As Integer = -1 ' array index
            Console.WriteLine("Enter the string: ")
            input = Console.ReadLine()
            l = Len(input)
            For i As Integer = 1 To l
                temp = Mid(input, i, 1)
                'if temp is a capital letter increase the array index by 1 and add temp to that array item
                If (Asc(temp) >= 65 And Asc(temp) <= 90) Then
                    z = z + 1
                    str(z) = str(z) & temp
                End If
                ' if the temp is lower case then just add temp to the current array item
                If (Asc(temp) >= 97 And Asc(temp) <= 122) Then
                    str(z) = str(z) & temp
                End If
            Next
            Console.WriteLine()
    
            For a As Integer = 0 To 3
                Console.WriteLine(str(a))
            Next a
            Console.ReadKey()
        End Sub
    End Module
    

    我应该解释为什么 Z 以 -1 开头。这是基于输入字符串的第一个字母是大写的假设。

    当你第一次循环时,temp 中存储的第一个字符是大写的,第一个If 语句的内容被执行,所以 1 被添加到 z 使得 z=0 .然后将这个大写的第一个字母添加到数组的第一个元素 str(0) 中。

    当您继续循环时,后续的小写字母只会添加到 str(0)。

    当循环到达下一个大写字母时,再次将 1 添加到 z 以便 z=1 并将大写字母添加到 z(1) 等等。

    【讨论】:

    • 非常感谢。我假设 97 和 122 是小写字母的键?另外,你能解释一下'z'位吗?再次感谢。
    • 我已经更新了我的答案,以便查看此问题的其他人可以比在 cmets 中更明显地看到它:)
    【解决方案2】:

    显然,您还没有空运行代码。它充满了错误,因此它永远不会按预期运行。

    Dim str(5) As String
    For z As Integer = 1 To 5  ' will never run for over 5 words
    

    在下一行,我认为您的意思是使用Mid(input, i , 1)1 不是 ll 会给你整个字符串,而不仅仅是一个字母。

            temp = Mid(input, i, l)
            temp2 = Mid(input, i + 1, l)
    

    此行不会考虑AZ。你应该使用&gt;=&lt;=

            If Asc(temp) >= 65 And Asc(temp) <= 90 Then
    

    此行将返回错误或最后一个字符为空字符串

    temp2 = Mid(input, i + 1, l)
    

    这一行不会考虑数组中的第一个元素

    For a As Integer = 1 To 5
        Console.WriteLine(str(a))
    Next a
    

    您似乎受限于使用原生 VB6 函数的要求,尽管 VB.net 的功能可以帮助您以更少的行更简洁地编写此代码。

    下面的代码,同样限制在 5 个字,应该给你你需要的输出:

    Sub Main()
        Dim input As String
        Dim str(5) As String
        Dim tempstr As String
        Dim temp As Char
        Dim temp2 As Char
        Dim l As Integer
        Dim arrCounnter As Integer
        Console.WriteLine("Enter the string: ")
        input = Console.ReadLine()
        tempstr = ""
        l = Len(input)
    
        For i As Integer = 1 To l
            temp = Mid(input, i, 1)
            'If capital, add to new temp; put old temp in array
            If Asc(temp) >= 65 And Asc(temp) <= 90 Then
                If tempstr <> "" Then
                    str(arrCounnter) = tempstr
                    arrCounnter = arrCounnter + 1
                End If
                tempstr = temp
            Else
                'If not, add to old temp, nxt
                tempstr = tempstr & temp
            End If
            If i = l Then str(arrCounnter) = tempstr
        Next i
    
        For a As Integer = 0 To 5
            If str(a) = "" Then
                Console.WriteLine("(Empty)")
            Else
                Console.WriteLine(str(a))
            End If
        Next a
        Console.ReadKey()
    End Sub
    

    【讨论】:

    • 非常感谢。我以前解决过这类问题,但昨晚我无法专注于任何事情,只是做了一个毫无意义的填充代码。按照我的理解,arrCounter 是跳过一个元素,然后在条件为真时连接字符串?我之前尝试过,但使用相同的变量(temp2)替换它失败了,最终没有给出想要的结果。
    • @Nick,Temp2 实际上有一个 char..a 字符串.. 存储在其中。 ArrCounter 将位置保持为输出数组中的索引。当检测到一个新单词时,它将存储在 ArrCounter 指定的索引处,然后 ArrCounter 为下一个单词递增。只有 temp 和 tempstr 用于连接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    相关资源
    最近更新 更多