例如: String = Visual BasiC 输出= V C
我尝试过到处搜索但没有找到,vb.net是否可以这样做?
答案 0 :(得分:0)
以下代码在字符串中向前迭代,直到找到大写字母,将其添加到结果并退出第一个循环。然后它在字符串中向后迭代,直到找到大写字母,将其添加到结果并退出循环。最后,它将结果返回给调用代码。
我怀疑这是学校/大学的任务,所以我建议您阅读Open letter to Students with homework problems
Private Function FirstAndLastCapitalLetter(s As String) As String
Dim result As String = ""
For i As Integer = 0 To s.Length - 1
If s.Substring(i, 1) = s.Substring(i, 1).ToUpper Then
result = result & s.Substring(i, 1)
Exit For
End If
Next
For i As Integer = s.Length - 1 To 0 Step -1
If s.Substring(i, 1) = s.Substring(i, 1).ToUpper Then
result = result & s.Substring(i, 1)
Exit For
End If
Next
Return result
End Function