【问题标题】:Find the word in the line, the beginning of a specific letter and the word, the end of the same letter在行中查找单词,特定字母的开头和单词,相同字母的结尾
【发布时间】:2019-05-24 15:35:29
【问题描述】:

给定一个带有单词的字符串,由问题分隔,我需要找出哪个单词出现在之前:哪个单词以字母“k”开头或以“k”结尾

我试过用 Mid 和 Len 运算符来做这个,但现在我需要用 Substring 和 Length 来做这个

Sub uncorrect(ByVal s As String, ByRef res As String)
    Dim i As Integer
    Dim t, w As String
    s = s + " "
    w = ""
    For i = 1 To Len(s)
        t = Mid(s, i, 1)
        If t = " " Then
            If (Mid(w, 1, 1) = "k") Then
                res = "Word, begin with k"
                Exit Sub
            Else
                If (Mid(w, Len(w), 1) = "l") Then
                    res = "Word, end with k"
                    Exit Sub
                End If
            End If
            w = ""
        Else
            w = w + t
        End If
    Next
End Sub

Sub trying(ByVal s As String, ByRef res As String)
    Dim i As Integer
    Dim mass = s.Split(" ")
    For i = 0 To mass.Length - 1
        If mass(i).Substring(1, 1) = "k" Then
            res = "Word, begin with k"
            Exit For
        Else
            If mass(i).Substring(mass(i).Length - 1, 1) = "k" Then
                res = "Word, end with k"
                Exit For
            End If
        End If
    Next
End Sub

实际输出非常不稳定:或者结果文本框为空,或者错误

【问题讨论】:

  • 谢谢大家 但是我发现了一个错误mass(i).Substring(1, 1) = "k" => mass(i).Substring(0, 1) = "k"

标签: arrays string vb.net


【解决方案1】:

如果只需要一个字符,则不需要使用子字符串。只需使用默认属性指定索引即可获取字符。

Sub trying(ByVal s As String, ByRef res As String)
    Dim i As Integer
    Dim mass = s.Split(" "c)
    For i = 0 To mass.Length - 1
        If mass(i)(0) = "k"c Then
            res = "Word, begin with k"
            Exit For
        Else
            If mass(i)(mass(i).Length - 1) = "k"c Then
                res = "Word, end with k"
                Exit For
            End If
        End If
    Next
End Sub

【讨论】:

    【解决方案2】:

    我使用 Linq 创建了一个函数,该函数返回一个 List(Of String),其中包含符合条件的单词。我修剪了任何标点符号的单词,并用 String.StartsWith 和 String.EndsWith 进行了测试。出于测试目的,我将结果打印到立即窗口。

    Private Function GetWords(input As String) As List(Of String)
        Dim words = input.Split(" "c)
        Dim Kwords = (From S In words
                      Let TrimmedS = S.Trim(New Char() {","c, "."c}) 'and whatever other puncuation you need to trim
                      Where TrimmedS.StartsWith("K", StringComparison.OrdinalIgnoreCase) OrElse TrimmedS.EndsWith("K", StringComparison.OrdinalIgnoreCase)
                      Select TrimmedS).ToList
        Return Kwords
    End Function
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim KWords = GetWords("The quick fox")
        Debug.Print("The quick fox")
        For Each w In KWords
            Debug.Print(w)
        Next
        Dim KWords1 = GetWords("Kick, scream, and knock out.")
        Debug.Print("Kick, scream, and knock out.")
        For Each w In KWords1
            Debug.Print(w)
        Next
    End Sub
    

    结果:

    The quick fox  
    quick  
    Kick, scream, and knock out. 
    Kick 
    knock
    

    【讨论】:

    • 这可能是最现代的解决方案,但老师不会接受,理由是“Linq not explain in Lectures”
    • @Lophi 真可惜。主动超越课堂的学生应该得到奖励。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 2015-07-14
    • 2017-04-13
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    相关资源
    最近更新 更多