在VB中使用子串和ToUpper的问题

时间:2015-12-13 04:32:17

标签: vb.net

我正在为一个小程序上课,错误是,特别是与第18行有关的错误:

  

指数超出范围。必须是非负数且小于集合的大小。

我对代码中出现的问题感到困惑。具体来说,这个循环失败了呢?

Do
        CurrentPositionOfAtSymbol = StartingTweet.IndexOf("@", CurrentPositionOfSpace)
        CurrentPositionOfSpace = StartingTweet.IndexOf(" ", CurrentPositionOfAtSymbol)
        StartingTweet.Substring(CurrentPositionOfAtSymbol + 1, CurrentPositionOfSpace - CurrentPositionOfAtSymbol).ToUpper()
    Loop Until CurrentPositionOfAtSymbol = -1

这是整个代码块:

Public Class Form1

    Dim Tweets As New ArrayList
    Dim Temp As String = ""
    Dim UserName As String
    Dim NumberOfCharacters As Integer
    Dim StartingTweet As String
    Dim FinishedTweet As String
    Dim AmountOfTweets As Integer
    Dim CurrentPositionOfAtSymbol As Integer = 0
    Dim CurrentPositionOfSpace As Integer = 0
    Dim UppercasedName As String

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        'place tweet into variable
        StartingTweet = TweetTextBox.Text
        'make all others usernames uppercase
        Do
            CurrentPositionOfAtSymbol = StartingTweet.IndexOf("@", CurrentPositionOfSpace)
            CurrentPositionOfSpace = StartingTweet.IndexOf(" ", CurrentPositionOfAtSymbol)
            StartingTweet.Substring(CurrentPositionOfAtSymbol + 1, CurrentPositionOfSpace - CurrentPositionOfAtSymbol).ToUpper()
        Loop Until CurrentPositionOfAtSymbol = -1
        'place username before tweet
        FinishedTweet = UserName + Environment.NewLine + StartingTweet
        'add date & time to tweet
        FinishedTweet = FinishedTweet + Environment.NewLine + DateAndTime.DateString
        'add the tweet to the arraylist
        Tweets.Add(FinishedTweet)
        'display all tweets in the arraylist
        TweetedTweetsBox.Text = String.Join(Environment.NewLine + " " + Environment.NewLine, Tweets.ToArray)
        'clear out the textbox for a new tweet
        TweetTextBox.Text = ""
        'Increase and display number of tweets
        AmountOfTweets = AmountOfTweets + 1
        NumberOfTweets.Text = AmountOfTweets.ToString
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'ask the user for name and store it as a string
        UserName = InputBox("What is your username?")
        'replace name spaces with hyphens
        UserName = UserName.Replace(" ", "-")
        'make name totally lowercase
        UserName = UserName.ToLower()
        'adds @ to username
        UserName = "@" + UserName
        'displays username
        UserNameDisplay.Text = UserName
    End Sub

    Private Sub TweetTextBox_TextChanged(sender As Object, e As EventArgs) Handles TweetTextBox.TextChanged

        'find the amount of characters in the text box 
        NumberOfCharacters = Len(TweetTextBox.Text)
        'output the amount of characters in the text box
        CurrentCharacterAmount.Text = NumberOfCharacters.ToString
    End Sub

End Class

1 个答案:

答案 0 :(得分:0)

  public class Main {

public static void main(String[] args) {

    String[][] words = {{"rice", "egg", "room"},
    {"apple", "java", "owl"}};

    for (int i = 0; i < words.length; i++) { //number of rows
        for (int j = 0; j < words[i].length; j++) { //number of columns
            if (j > 0) { //if it's not in the first column
                //if it starts w a vowel
                if (words[i][j].substring(0, 1).equals("a") || words[i][j].substring(0, 1).equals("e") || words[i][j].substring(0, 1).equals("i") || words[i][j].substring(0, 1).equals("o") || words[i][j].substring(0, 1).equals("u")) {
                    //if it starts w a vowel, it swaps with the word 1 over to the left
                    swapWord(words, i, j, i, j - 1);
                }
            } else if (j == 0) { //if it is in the first column
                if (i > 0) { 
                    //swaps with the word directly above it
                    swapWord(words, i, j, i - 1, j);

                }
            }
            //swaps based on consonance
            if (!words[i][j].substring(0, 1).equals("a") && !words[i][j].substring(0, 1).equals("e") && !words[i][j].substring(0, 1).equals("i") && !words[i][j].substring(0, 1).equals("o") && !words[i][j].substring(0, 1).equals("u")) {
                words[i][j] = swapLetter(words[i][j]);
            }

        }

    }
    //outputs the newly shifted array to the console
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            System.out.print(words[i][j] + " ");
        }
        System.out.println();
    }
}

//swaps a word with another word
public static void swapWord(String[][] a, int i1, int j1, int i2, int j2) {
    String temp = a[i1][j1];
    a[i1][j1] = a[i2][j2];
    a[i2][j2] = temp;
}
//swaps the first letter of a string with the last letter of the string
public static String swapLetter(String s) {
    String newS = "";
    newS += s.substring(s.length() - 1);
    newS += s.substring(1, s.length() - 1);
    newS += s.substring(0, 1);
    return newS;
}

即使Do CurrentPositionOfAtSymbol = StartingTweet.IndexOf("@", CurrentPositionOfSpace) CurrentPositionOfSpace = StartingTweet.IndexOf(" ", CurrentPositionOfAtSymbol) StartingTweet.Substring(CurrentPositionOfAtSymbol + 1, CurrentPositionOfSpace - CurrentPositionOfAtSymbol).ToUpper() Loop Until CurrentPositionOfAtSymbol = -1 CurrentPositionOfAtSymbol,您的代码也会执行嵌套块的第2行和第3行。

此外,-1返回字符串的新实例,它不会改变原始值(字符串在.NET中是不可变的)。与Substring相同。

最后,虽然使用详细标识符有助于程序可维护性,但过于冗长会妨碍可读性,例如,请考虑使用.ToUpper()等名称而不是atSymbolIndex

更新:如果我正确理解您的代码,则意味着这样做:

  1. 查找所有twitter currentPositionOfAtSymbol - 带前缀的用户名并将其转换为大写。
  2. 从文本中提取它们并将它们移动到推文的开头。
  3. 所以这条推文:

      

    Hello @ username1你好吗? @ username2说你好

    成为这个:

      

    @ USERNAME1 @ USERNAME2你好,你好吗?问好吗

    我会使用一个简单的状态机解析器来完成此操作,该解析器通过从输入中读取每个字符(它有两个状态:@in-username)并在其进行转换时进行转换。我会使用C#,因为我对这种语言感觉更舒服,你应该能够很容易地遵循它:

    in-nonUsernameText