简体   繁体   English

VB.NET中的Val()函数如何工作?

[英]How does the Val() function in VB.NET work?

I have an assignment in VB.NET that I'm stuck with at the moment. 我目前在VB.NET中有一个任务,一直在坚持。 Would love some help. 希望有所帮助。

The question is this: You enter random characters into a textbox, for example 12ab3c4d5efgh, and at the click of a button, it must sort the characters in the textbox into 2 separate Labels, depending on whether or not the 'character' is a number or letter. 问题是这样的:您在文本框中输入随机字符,例如12ab3c4d5efgh,然后单击按钮,它必须将文本框中的字符分为2个单独的标签,具体取决于“字符”是否为数字或字母。 So, continuing the example, Label1 must show '12345' and Label 2 must show 'abcdefgh'. 因此,继续该示例,Label1必须显示“ 12345”,Label 2必须显示“ abcdefgh”。 I hope I made myself clear enough. 我希望我已经足够清楚了。

I was asked to use the Val() function but I really have no clue. 我被要求使用Val()函数,但我真的不知道。 Could someone please help? 有人可以帮忙吗? :D :D

This creates one string with the digits and one with the letters. 这将创建一个带数字的字符串和一个带字母的字符串。 Characters that are not digits or letters are ignored. 非数字或字母的字符将被忽略。

Dim chars As String = "12ab3c4d5efgh"
Dim nums As String = chars.Where(Function(c) Char.IsDigit(c)).ToArray 
Dim lets As String = chars.Where(Function(c) Char.IsLetter(c)).ToArray 

If you have to use Val() something like this would do. 如果必须使用Val()可以这样做。 But be careful: Val("0") also returns 0 . 但请注意: Val("0")也返回0

Dim numbers As String = String.Empty
Dim letters As String = String.Empty
Dim sourceString As String = "12ab3c4d50efgh"

For Each c As Char In sourceString
    If Val(c) = 0 And c <> "0" Then letters &= c Else numbers &= c
Next

Console.WriteLine("Numbers: " & numbers)
Console.WriteLine("Letters: " & letters)
Console.ReadKey()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM