对于我在uni的一个编程作业,我需要创建一个vb程序,它接受一个单词并使其复数。然后,我们必须写下我们面临的困难等
我只想弄清楚如何解决这个问题。从我所看到的,你首先必须将这个词分成它的字符,然后确定字符串的最后一个字符是什么。从那里我认为这是一个if语句加载的情况,它使用string.join运算符在最后添加像“s”这样的东西。加入似乎相当简单,但是它是如何将输入词分开并识别出最让我烦扰的字符。
我必须提一下,我只是在大学的第一年,所以我在编程方面都不是很先进,所以如果你能提供帮助,请尽可能让那些知识有限的人理解!< / p>
提前致谢, 安德鲁
答案 0 :(得分:0)
如果您只想添加&#34; s&#34;字符串到字符串的末尾
Dim myString As String = "Shoe"
myString += "s"
'myString is now "Shoes"
或者你想检查最后一个角色是否已经是&#34; s&#34;,更改&#34;可爱的&#34; to&#34; cuties&#34;等?
答案 1 :(得分:0)
如果这是一个关于添加&#39;的话题。到最后的字符我建议使用String.Format
并添加&#39; s&#39; :
Public Function pluralize(ByVal DesignString AS String) AS String
Return String.Format("{0}s",DesignString) ' ex : Shoe -> Shoes
End Function
跳这可以帮助你。
答案 2 :(得分:0)
就像迈克和威尔所说的有很多可能性,这段代码仅适用于某些情况,它使用If
语句,但Case
语句可能更好
Protected Sub btnPlural_Click(sender As Object, e As EventArgs) Handles btnPlural.Click
Dim strWord As String
Dim lastLetter As String
Dim secondlast As String
If txtWord.Text <> "" Then
strWord = txtWord.Text
lastLetter = GetChar(strWord, strWord.Length)
secondlast = GetChar(strWord, strWord.Length - 1)
If lastLetter = "r" Or lastLetter = "p" Or lastLetter = "t" Then
strWord = strWord + "s"
lblPlural.Text = strWord
End If
If lastLetter = "e" Then
If secondlast = "f" Then
strWord = strWord.Substring(0, strWord.Length - 2) + "ves"
lblPlural.Text = strWord
Else
strWord = strWord + "s"
lblPlural.Text = strWord
End If
End If
If lastLetter = "x" Then
strWord = strWord + "es"
lblPlural.Text = strWord
End If
If lastLetter = "h" Then
If secondlast = "c" Then
strWord = strWord + "es"
lblPlural.Text = strWord
Else
strWord = strWord + "s"
lblPlural.Text = strWord
End If
End If
If lastLetter = "y" Then
If secondlast = "t" Or secondlast = "d" Or secondlast = "n" Then
strWord = strWord.Substring(0, strWord.Length - 1) + "ies"
lblPlural.Text = strWord
ElseIf secondlast = "e" Or secondlast = "a" Or secondlast = "o" Then
strWord = strWord + "s"
lblPlural.Text = strWord
End If
End If
End If
End Sub
希望这有帮助