【发布时间】: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"