主要是为了练习。这是VB中的代码:
Private Function ScrambleWord(ByVal word As String) As String
Dim i As Integer = 0
Dim builder As System.Text.StringBuilder = New System.Text.StringBuilder()
Dim random As Random = New Random()
Dim index As Integer = 0
Dim lower As Integer = 0
Dim upper As Integer = 0
Dim parts() As Char
Dim part As Char
If Not (String.IsNullOrEmpty(word)) Then
If (word.Length > 3) Then
parts = word.ToCharArray()
builder.Append(word.Substring(0, 1))
parts = word.Substring(1, word.Length - 2).ToCharArray()
lower = LBound(parts) : upper = UBound(parts)
For i = lower To upper
index = random.Next(lower, upper)
part = parts(index)
parts(index) = parts(i)
parts(i) = part
Next
builder.Append(parts)
builder.Append(word.Substring(word.Length - 1, 1))
Return builder.ToString()
Else
Return word
End If
Else
Return String.Empty
End If
End Function
我使用了一个在线翻译网站并以此结束:
private string ScrambleWord(string word)
{
int i = 0;
StringBuilder builder = new StringBuilder();
Random random = new Random();
int index = 0;
int lower = 0;
int upper = 0;
char[] parts = null;
char part = '\0';
if (!(string.IsNullOrEmpty(word)))
{
if ((word.Length > 3))
{
parts = word.ToCharArray();
builder.Append(word.Substring(0, 1));
parts = word.Substring(1, word.Length - 2).ToCharArray();
lower = Information.LBound(parts);
upper = Information.UBound(parts);
for (i = lower; i <= upper; i++)
{
index = random.Next(lower, upper);
part = parts[index];
parts[index] = parts[i];
parts[i] = part;
}
builder.Append(parts);
builder.Append(word.Substring(word.Length - 1, 1));
return builder.ToString();
}
else
{
return word;
}
}
else
{
return string.Empty;
}
}
我不确定所有的翻译是否都很好,但现在我得到了两个错误,因为 C# 中不存在信息
错误出现在以下行的 C# 代码中:
lower = Information.LBound(parts);
upper = Information.UBound(parts);
两个错误都是一样的:
当前上下文中不存在名称“信息”
我从此链接获取了 VB 代码并尝试将其转换为 c#