简体   繁体   English

将VB.NET字符串操作转换为C#

[英]Translating VB.NET string manipulation to C#

I am trying to convert VB.NET code to C# code. 我正在尝试将VB.NET代码转换为C#代码。

This is my VB.NET code: 这是我的VB.NET代码:

Dim Part1 As String = "Some string"
Dim p_str As String
For I = 1 To Len(Part1)
    p_str = Chr(Asc(Mid$(Part1, I, 1)) + 17)
Next

I have translated it to C# like this: 我已经将它翻译成C#:

string Part1 = "Some string";
string p_str = null;
for (I = 0; I <= Part1.Length - 1; I++)
{
    p_str = Convert.ToChar((Part1.IndexOf(Part1.Substring(I, 1)) + 65) + 17).ToString();
}

Can anyone tell me whether it's correct? 谁能告诉我这是否正确?

You have to add a refererence to Microsoft.Visualbasic (Top menu, under "Add Reference" and the ".NET" Tab) and use Strings.Chr and Strings.Asc if you want to emulate perfectly the behaviour of Chr and Asc (as warned in this link by Garry Shutler's answer). 如果您想完美地模拟ChrAsc的行为,则必须向Microsoft.Visualbasic (“添加引用”和“ .NET”选项卡下的顶部菜单)添加引用,并使用Strings.ChrStrings.Asc Garry Shutler的回答警告此链接 )。 Your code would become: 您的代码将变为:

string Part1 = "Some string";
string p_str = null;
for (int I = 0; I < Part1.Length; I++)
{
    p_str = Strings.Chr(Strings.Asc(Part1.Substring(I, 1)) + 17).ToString();
}

CLARIFICATION 1: the original code you are posting is not (pure) VB.NET , it is VB (VB.NET allows most of the VB commands to be written in). 澄清1:您发布的原始代码不是(纯)VB.NET ,而是VB(VB.NET允许写入大多数VB命令)。 That's why so many changes are required (for example: changing the starting index); 这就是为什么需要这么多更改的原因(例如:更改起始索引); VB.NET is much more similar to C#.NET than this (as shown below). VB.NET与C#.NET更相似(如下所示)。

CLARIFICATION 2: it is generally accepted that the C# translations for VB's Asc and Chr are mere casts (to int and char respectively), as you can see in the most voted answer in the aforementioned link. 澄清2:通常可以接受的是,VB的AscChr的C#转换仅是强制转换(分别转换为intchar ),如您在上述链接中获得最多投票的答案所示。 THIS IS WRONG . 这是错的 Test this code with both alternatives to confirm that only the Strings options deliver always the right result. 使用两种替代方法测试此代码,以确认只有Strings选项始终提供正确的结果。

"PROPER" VB.NET CODE: “PROPER”VB.NET代码:

Dim Part1 As String = "Some string"
Dim p_str As String = Nothing
For I As Integer = 0 To Part1.Length - 1
    p_str = Chr(Asc(Part1.Substring(I, 1)) + 17)
Next

You don't need a reference to Microsoft.VisualBasic (edit: see the comments - in this case you do need it), and your 'for' loop condition should be "I <= Part1.Length": 您不需要对Microsoft.VisualBasic的引用(编辑:请参见注释-在这种情况下,您确实需要它),并且您的“ for”循环条件应为“ I <= Part1.Length”:

string Part1 = "Some string";
string p_str = null;
for (int I = 1; I <= Part1.Length; I++)
{
    'edit: I thought this would work, but it doesn't:
    p_str = ((char)(Convert.ToInt32(Part1[I - 1]) + 17)).ToString();
    'edit: the following works, with "Strings.Chr" and "Strings.Asc" remaining:
    p_str = Microsoft.VisualBasic.Strings.Chr(Microsoft.VisualBasic.Strings.Asc(Part1[I - 1]) + 17).ToString(); 
}

pretty much but you need to declare the I variable in your for loop. 但是你需要在for循环中声明I变量。 Correction: 更正:

for (int I = 0; I <= Part1.Length-1; I++)
 {
...

 }

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

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