简体   繁体   English

拆分PascalCase字符串并将其存储在VB中的数组时遇到麻烦

[英]Having trouble splitting a PascalCase string and storing it in an array in VB

CorrectHorseBatteryStaple

to

Correct
Horse
Battery
Staple
(Empty)

There's also a catch, I can't use classes, functions, built-in functions other than Mid(), Right(), Left(), Len(), Asc(). 还有一个问题,除了Mid(),Right(),Left(),Len(),Asc(),我不能使用类,函数,内置函数。 Which makes the whole thing much more difficult. 这使整个事情变得更加困难。

I can't for the life of me figure out how to compare the characters in the string and somehow stop the loop/store the first word in the array and so on. 我一辈子都想不通如何比较字符串中的字符,以某种方式停止循环/将第一个单词存储在数组中,依此类推。

Here's what I've done so far, which isn't anything meaningful: 到目前为止,这是我所做的,没有任何意义:

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

Clearly, you haven't dry run the code. 显然,您还没有空运行代码。 It's littered with mistakes because of which it will never run as intended. 它到处都是错误,因此它永远不会按预期运行。

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

on this next line, I think you meant to use Mid(input, i , 1) . 在下一行中,我认为您打算使用Mid(input, i , 1) 1 not l . 1不是l l will give you the whole string and not just a single letter. l会给你整个字符串,而不仅仅是一个字母。

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

This line won't consider A and Z . 这条线不会考虑AZ you should use >= and <= 您应该使用>=<=

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

This line will return an error or an empty string on the last character 该行将在最后一个字符上返回错误或空字符串

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

This line won't consider the first element in the array 该行将不考虑数组中的第一个元素

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

It looks like you've been limited by your requirement to using native VB6 functions, although VB.net's functionality would help you to write this more cleanly and in fewer lines. 尽管VB.net的功能可以帮助您更清晰,更少地编写代码,但看起来似乎受限于使用本机VB6函数的要求。

The below code, again limited to 5 words, should give you the output you need: 下面的代码,再次限制为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

Before I start, I would suggest that you use a list instead of an array. 在开始之前,我建议您使用列表而不是数组。 That way, if you want to split more words, you wont need to change the code. 这样,如果您想分割更多的单词,则无需更改代码。 However, I'm guessing you haven't covered those yet. 但是,我想您还没有涵盖这些内容。 So... 所以...

The simplest way would be to loop through each character of the array and if the character is upper case then move on to the next array item and add that character to the array item. 最简单的方法是遍历数组的每个字符,如果该字符是大写字母,则继续进行下一个数组项并将该字符添加到数组项。 If the character is lower case then just add it to the current array item. 如果字符是小写字母,则只需将其添加到当前数组项中即可。 You don't need to use so many variables this way. 您无需通过这种方式使用太多变量。

There is an assumption here that the first letter will be upper case. 此处假设第一个字母为大写。 if it isn't there will be an 如果不是,将会有一个

Index out of range 索引超出范围

error. 错误。


Here you go .. 干得好 ..

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

I should explain why Z starts out as -1. 我应该解释为什么Z以-1开始。 This is based on the assumption that the first letter of the input string is upper case. 这是基于以下假设:输入字符串的第一个字母为大写。

As you go through the loop for the first time, the first character that got stored in temp is upper case and the contents of the first If statement are executed, so 1 is added to z making z=0. 第一次循环时,存储在temp的第一个字符为大写字母,并且执行了第一个If语句的内容,因此将1添加到z使得z = 0。 Then this first letter which is upper case is added to str(0) which is the first element of the array. 然后,将大写的第一个字母添加到数组的第一个元素str(0)中。

As you carry on through the loop, the subsequent lower case letters merely get added to str(0). 在进行循环时,后续的小写字母仅会添加到str(0)中。

When the loop reaches the next upper case letter, 1 is added to z again so that z=1 and the upper case letter is added to z(1) and so on. 当循环到达下一个大写字母时,将1再次添加到z ,以便z = 1并将大写字母添加到z(1),依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM