【问题标题】:Text String with Delegates in VB.NetVB.Net 中带有委托的文本字符串
【发布时间】:2011-05-23 20:34:50
【问题描述】:

我为我的 VB.Net 编程课程分配了以下项目:

“使用各种程序编写一个程序来执行下面列出的操作。使用委托调用这些程序。确保记录您的程序并让程序打印描述性文本以及 b. 和 c 中的数字。

a) Print a text string in reverse word order. 
b) Print the number of characters in the string.
c) Print number of words in the string."

现在,我提出了几个与我应该如何完成作业有关的问题(其中一些是基于意见的)。

首先,你们认为我的老师所说的“反向词序”是什么意思?他们的意思是打印一个单词组合向后的文本字符串(即“siht si a ecnetnes”),他们的意思是打印一个整个单词向后的文本字符串(即“句子 a is this”),还是他们的意思两者同时(即“ecnetnes a si siht”)?这是基于意见的问题之一,但我只是想听听你们的想法。

其次,产生字符串中字符数的语法是什么?我已经知道获取字数所需的代码,但是这个作业的 b 部分让我有点困惑。任何帮助将不胜感激。

【问题讨论】:

  • 我对a)的看法是老师希望每个单词的顺序倒序,所以“句子a is this”是正确的。
  • 其实“反向词序”也可能意味着按字母降序显示字符串,所以你的示例字符串“这是一个句子”,可以是this sentence is a
  • 你的第一个问题其实还不错。但我们只能猜测,和你一样。问你的老师。 ;-)

标签: vb.net string delegates count


【解决方案1】:

对于第二个问题,产生字符串中字符数的语法是:

Dim mystring as String = "This is a string"
Console.Writeline(mystring.Length)

// outputs 16

正如我在 cmets 中提到的,我对您的第一个问题的猜测是,老师希望将单词反转,而不是字符,因此“this is a sentence”将反向显示为“sentence a is this”

【讨论】:

  • 不过,我觉得老师想知道字母的个数,舍弃空格。
  • Length 是一个属性,而不是一个方法。
  • @Paulo 作业说的是“字符数”,而不是“字母”。
  • @Paulo,很有趣。我想这是一个有趣的练习,但不是很实用(在我的职业生涯中,我想知道一个字符串中有多少个非空白字符)。在这种情况下,您可以使用Replace(" ", "") 方法删除空格,然后获取长度。
  • 感谢大家的帮助。至于“a)”部分,我想我会坚持将单词保持原样,并将句子向后显示(如“sentence a is this”)。
【解决方案2】:

因为听起来很有趣,所以快速尝试了一下。

    '   Reverse the string and then print it to the output window
    Dim ReverseArray As Array = "Print a text string in reverse word order.".ToCharArray

    '   Reverse the array to
    Array.Reverse(ReverseArray)

    '   Convert the array back into a string (should be Reversed...)
    Debug.WriteLine("Reversed string = '{0}'", New String(ReverseArray))

    '   Get the number of Characters, remember a "Space" is still a Character
    Debug.WriteLine("Number of characters in array = {0}", ReverseArray.Length)

    '   Count the number of spaces in the string, then add an extra one
    Debug.WriteLine("Number of Words = {0}", (From c In ReverseArray Where c.ToString = " " Select c).Count + 1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多