格式化功能-VB6到C#的转换

问题描述 投票:0回答:1

什么是C#等效项:

Public stringValue As String
Public intValue As Integer

intValue = Format(val(stringValue), "00")

c# vb6
1个回答
0
投票

因此vb6 Format函数将转换为字符串,而Val函数将转换为数字。如果您查看Microsoft documentation for Val

Val("    1615 198th Street N.E.")
// returns
1615198

在c#中,可以很笨拙地转换为

string stringValue = "    1615 198th Street N.E.";

// assign to int if int wanted
// throws exception if cannot be converted to int, use Int32.TryParse if you want to handle that
int intValue = System.Convert.ToInt32(
    new string(stringValue.Replace(" ", "").TakeWhile(System.Char.IsDigit).ToArray())
);

System.Console.WriteLine(intValue);

这实际上取决于您对stringValue将会是什么以及您想要返回什么的期望。另外,我不认为您可以为string分配int值,所以像在vb6中那样随意。它肯定可以清除,但这仅取决于您在做什么。

© www.soinside.com 2019 - 2024. All rights reserved.