-3

我已经尝试了很多次,虽然我可以创建代码,使其能够用于诸如“find”或“else”之类的单词,但我无法使其适用于以两个或多个辅音开头的单词. 我的具体问题是有没有一种方法可以让程序搜索 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

结束类

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

4

3 回答 3

2

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

于 2013-06-23T00:01:32.850 回答
1

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

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

    var index = word.IndexOfAny(vowels);
于 2013-06-23T00:04:24.643 回答
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

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

去做:

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

于 2013-06-23T07:35:42.500 回答