vb.net 在字符串中查找元音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17256326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:00:46  来源:igfitidea点击:

Finding a vowel in a string

vb.net

提问by Kunal Grover

I've given it a number of tries and while I can create code that will enable me to make it work for words such as "find" or "else" I'm unable to make it work for words starting with two or more consonants. My specific question would be is there a way I can make the program search for one of a,i,o,u, or e? so then I can request the position of the first instance of their use, using IndexOf + Substring to complete the question.

我已经进行了多次尝试,虽然我可以创建代码,使其能够适用于诸如“find”或“else”之类的单词,但我无法使其适用于以两个或更多辅音开头的单词. 我的具体问题是有没有办法让程序搜索 a、i、o、u 或 e 之一?那么我可以请求他们使用的第一个实例的位置,使用 IndexOf + Substring 来完成这个问题。

my code so far is :-

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

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

End Class

结束班

Any help or advice will be really appreciated

任何帮助或建议将不胜感激

回答by Jerry

You can use a regular expression such as [aeiou]. Then use the Indexproperty to get the location after matching

您可以使用正则表达式,例如[aeiou]. 然后使用Index属性获取匹配后的位置

回答by EddDotNet

If im understanding the question, you could use; e.g

如果我理解这个问题,你可以使用;例如

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

    var index = word.IndexOfAny(vowels);

回答by tinstaafl

For what you're trying to do, here's one way:

对于您想要做的事情,这是一种方法:

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

I included code to maintain the case of the first letter

我包含了代码来维护第一个字母的大小写

TODO:

去做:

Use a list of words to validate the input to make sure it's a valid word.

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