1

ここで初心者ですが、ご容赦ください。間違いがある場合は事前にお詫び申し上げます。ちょっと手こずっている宿題です。

全体的な目標: ループ ステートメントを使用して文字列内の特定の文字数を出力する。例として、ユーザーが「ニワトリが道路を横切った理由」の中に「私」が何個あるかを知りたい場合、答えは 2 である必要があります。

1) フォーム/GUI には、1 つの MultiLine テキストボックスと「検索」というタイトルの 1 つのボタンがあります。

2)ユーザーがテキストボックスにテキストを入力/コピー/貼り付け、[検索]ボタンをクリックします

3) 検索ボタンは入力ボックスを開き、ユーザーはテキストボックスで検索したい文字を入力し、[OK] を押します。

4) (本当に助けが必要な場合)ループ ステートメントを使用すると、プログラムはInputboxに入力されたテキストがMultiLine Textbox内のテキストに表示される回数を検索してカウントし、文字が表示された回数を表示します「messagebox.show」で

私がこれまでに持っているすべて

Private Sub Search_btn_Click(sender As System.Object, e As System.EventArgs) Handles Search_btn.Click
    Dim counterInt As Integer = 0
    Dim charInputStr As String
    charInputStr = CStr(InputBox("Enter Search Characters", "Search"))
4

5 に答える 5

0

このコードを試してください....テストされていませんが、私は自分のvbを知っています:)

    Function lol(ByVal YourLine As String, ByVal YourSearch As String)
    Dim num As Integer = 0
    Dim y = YourLine.ToCharArray
    Dim z = y.Count
    Dim x = 0
    Do
        Dim l = y(x)
        If l = YourSearch Then
            num = num + 1
        End If
        x = x + 1
    Loop While x < z

    Return num
End Function

独自のカウンターを使用する関数です...文字列内のすべての文字に対して、その文字が設定したもの(YourSearch)であるかどうかを確認し、見つかったアイテムの数を返します。あなたの場合、行に2つのiがあるため、2が返されます。

お役に立てれば!

編集:

これは、単語ではなく個々の文字を検索している場合にのみ機能します

于 2013-04-02T08:15:47.843 に答える
0

String.IndexOf(string, int)メソッドを使用してそれを取得します。概念の簡単な例:

Dim input As String = "Test input string for Test and Testing the Test"
Dim search As String = "Test"

Dim count As Integer = -1
Dim index As Integer = 0

Do
    index = input.IndexOf(search, index) + 1
    count += 1
Loop While index > 0

countはループの使用-1のために で初期化されます -入力文字列にパターンの出現がない場合でも に設定されます。do-while0

于 2013-04-02T06:31:36.993 に答える
0

次のようなもので試すことができます:

Dim sText As String = TextBox1.Text
Dim searchChars() As Char = New Char() {"i"c, "a"c, "x"c}
Dim index, iCont As Integer 

index = sText.IndexOfAny(searchChars)

While index >= 0 Then
    iCont += 1
    index = sText.IndexOfAny(searchChars, index + 1)
End While

Messagebox.Show("Characters found " & iCont & " times in text")

単語と各単語が出現する時間を検索したい場合は、次のようにしてください。

Dim text As String = TextBox1.Text
Dim wordsToSearch() As String = New String() {"Hello", "World", "foo"}
Dim words As New List(Of String)()
Dim findings As Dictionary(Of String, List(Of Integer))

'Dividing into words
words.AddRange(text.Split(New String() {" ", Environment.NewLine()}, StringSplitOptions.RemoveEmptyEntries))

findings = SearchWords(words, wordsToSearch)
Console.WriteLine("Number of 'foo': " & findings("foo").Count)

この機能を使用すると:

Private Function SearchWords(ByVal allWords As List(Of String), ByVal wordsToSearch() As String) As Dictionary(Of String, List(Of Integer))
    Dim dResult As New Dictionary(Of String, List(Of Integer))()
    Dim i As Integer = 0

    For Each s As String In wordsToSearch
        dResult.Add(s, New List(Of Integer))

        While i >= 0 AndAlso i < allWords.Count
            i = allWords.IndexOf(s, i)
            If i >= 0 Then dResult(s).Add(i)
            i += 1
        End While
    Next

    Return dResult
End Function

出現回数だけでなく、ファイル内のインデックス位置も辞書で簡単にグループ化できます。

于 2013-04-02T09:15:21.267 に答える
0

このコードを試す

 Dim input As String = "Test input string for Test and Testing the Test"
        Dim search() As String = {"Te"}

        MsgBox(input.Split(input.Split(search, StringSplitOptions.None), StringSplitOptions.RemoveEmptyEntries).Count)
于 2013-04-02T07:57:43.723 に答える