在字符串中查找元音

时间:2013-06-22 23:31:46

标签: vb.net

我已经给了它一些尝试,而我可以创建代码,使我能够使它适用于诸如“查找”或“其他”之类的单词我无法使其适用于以两个开头的单词或更多辅音。我的具体问题是有没有办法让程序搜索a,i,o,u或e中的一个?所以我可以使用IndexOf + Substring来请求它们使用的第一个实例的位置来完成这个问题。

到目前为止,我的代码是: -

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

结束班

非常感谢任何帮助或建议

3 个答案:

答案 0 :(得分:2)

您可以使用正则表达式,例如[aeiou]。 然后使用Index属性在匹配

后获取位置

答案 1 :(得分:1)

如果我理解了这个问题,你可以使用; e.g

    char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };
    string word = "test";

    var index = word.IndexOfAny(vowels);

答案 2 :(得分:0)

对于你想要做的事,这是一种方式:

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

我提供了代码来维护第一个字母的情况

TODO:

使用单词列表验证输入,以确保它是有效的单词。