Vb.net 在字符串中查找元音
我已经尝试了很多次,虽然我可以创建代码,使它能够用于“find”或“else”等词,但我无法使它适用于以两个或更多辅音开头的词。我的具体问题是,有没有办法让程序搜索a、I、o、u或e中的一个?然后我可以请求它们使用的第一个实例的位置,使用IndexOf+子字符串来完成这个问题 到目前为止,我的代码是:-Vb.net 在字符串中查找元音,vb.net,Vb.net,我已经尝试了很多次,虽然我可以创建代码,使它能够用于“find”或“else”等词,但我无法使它适用于以两个或更多辅音开头的词。我的具体问题是,有没有办法让程序搜索a、I、o、u或e中的一个?然后我可以请求它们使用的第一个实例的位置,使用IndexOf+子字符串来完成这个问题 到目前为止,我的代码是:- Private Sub btnCompute_Click(sender As System.Object, e As System.EventArgs) Handles btnCompute.C
Private Sub btnCompute_Click(sender As System.Object, e As System.EventArgs) Handles btnCompute.Click
Dim word As String = CStr(txtInput.Text)
Dim first_letter As String = word.Substring(0, 1)
Const vovel As String = "aeiouy"
Const constants As String = "bcdfjklmnopqrstvwxz"
Dim find As String = word.Substring(0, vovel)
Dim delete As String = word.Substring(vovel, constants)
If vovel.Contains(first_letter) Then
txtResults.Text = txtInput.Text & "way"
ElseIf constants.Contains(first_letter) Then
txtResults.Text = delete & find & "ay"
End If
End Sub
末级
非常感谢您提供的任何帮助或建议您可以使用正则表达式,如
[aeiou]
。
然后使用
索引
属性在匹配后获取位置如果我理解问题,您可以使用;e、 g
char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };
string word = "test";
var index = word.IndexOfAny(vowels);
对于您试图做的事情,这里有一种方法:
Private Function IgpayAtinlay(word As String) As String
Dim vowels As String = "aeiou"
Dim Upper As Boolean = False
If Char.IsUpper(word(0)) Then Upper = True
For I = 0 To word.Length - 1
If vowels.Contains(word(I)) Then
If I = 0 Then
'If the vowel is the first letter add "way" to the end
word = word + "way"
Exit For
Else
'Otherwise we take the rest of the word starting at the vowel,
'put the beginning letters on the end and add "ay"
word = word.ToLower
word = word.Substring(I) + word.Substring(0, I) + "ay"
'If the first letter was upper case then make the new first letter
'the same
If Upper Then word = word(0).ToString.ToUpper + word.Substring(1)
Exit For
End If
End If
Next
Return word
End Function
Private Sub btnCompute_Click(sender As System.Object, e As System.EventArgs) Handles btnCompute.Click
txtResults.Text = IgpayAtinlay(txtInput.Text)
End Sub
我加入了代码来维护第一个字母的大小写
待办事项:
使用一个单词列表来验证输入,以确保它是一个有效的单词。您是否知道
字母
没有声明,而且第一个字母
从未使用过?一旦您更正了代码,您可能会发现这一点很有用:如果“aeiou”。包含(letter.ToLower()),那么我只是再次尝试,并发布了我的新代码,仍然没有编译.Substring()
不接受字符串参数!仅供参考,该词为“元音”,但区分大小写,也可使用C#而不是VB.NET。如果需要区分大小写:char[]元音=新字符[]{'a','a','e','e','i','i','o','u','u'}代码>