vb.net 翻转字符串的字母 - Visual Basic
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13225850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flip letters of a string - Visual Basic
提问by Ds.109
Simple coding assignment: Take text from a textbox and flip it so it's backwords:
简单的编码分配:从文本框中取出文本并将其翻转,使其成为倒词:
i.e. Hello My Name Is David would be "divad si eman ym olleh" ( The program doesn't have to match case, just the letters)
即 Hello My Name Is David 将是“divad si eman ym olleh”(该程序不必匹配大小写,只需匹配字母)
This is something I found, do you have any other methods?
这是我发现的,你还有其他方法吗?
Dim str As String = Textbox1.Text
Dim arr As New List(Of Char)
arr.AddRange(str.ToCharArray)
arr.Reverse()
For Each l As Char In arr
lblOne.Text &= l
Next
回答by sj1900
You can do it in one line with using the StrReversefunction (in Microsoft.VisualBasic).
您可以使用该StrReverse函数(在 Microsoft.VisualBasic 中)在一行中完成。
Dim myText As String = "My Name is Dave"
Dim revText As String = StrReverse(myText)
回答by Ceres
Quick one liner.
快速的一个班轮。
lblOne.Text = String.Join("", "divad si eman ym olleh".Reverse())
回答by javaid ahmad wani
Microsoft.VisualBasic
Microsoft.VisualBasic
Dim myText As String = My Name is abc
Dim revText As String = StrReverse(myText)
Output: "cba si eman ym"
输出:“cba si eman ym”
回答by Suji
The simplest method to reverse a string is :
反转字符串的最简单方法是:
Dim s As String = "1234ab cdefgh"
MessageBox.Show(s.AsEnumerable.Reverse.ToArray)
回答by Chirag Kachhadiya
- First Create a textbox, it will be TextBox1
- then create a button and name it Reverse
- then Create a label, it will be Label1
- Now double click on Reverse Button (Go to Button Click Event)
- and type following code.
and run software And type your string in textbox and click on reverse button.
Dim MainText As String = TextBox1.Text Dim revText As String = StrReverse(MainText) Label1.Text = revText
- 首先创建一个文本框,它将是 TextBox1
- 然后创建一个按钮并将其命名为 Reverse
- 然后创建一个标签,它将是 Label1
- 现在双击反向按钮(转到按钮单击事件)
- 并输入以下代码。
并运行软件并在文本框中键入您的字符串,然后单击反向按钮。
Dim MainText As String = TextBox1.Text Dim revText As String = StrReverse(MainText) Label1.Text = revText
回答by lc.
You can use String.Joininstead of looping through each character and concatenating:
您可以使用String.Join而不是遍历每个字符并连接:
lblOne.Text = String.Join("", arr)
回答by John Woo
create a functionthat accepts string an returns a reversed string.
创建一个function接受字符串并返回一个反向字符串的字符串。
Function Reverse(ByVal value As String) As String
Dim arr() As Char = value.ToCharArray()
Array.Reverse(arr)
Return New String(arr)
End Function
and try using it like this,
并尝试像这样使用它,
lblOne.Text = Reverse(Textbox1.Text)
回答by Ahmad
Here is a similar way but with fewer number of lines.
这是一种类似的方式,但行数较少。
Dim Original_Text As String = "Hello My Name is Ahmad"
Dim Reversed_Text As String = ""
For i = Original_Text.Length To 1 Step -1
Reversed_Text &= Original_Text.Substring(i, 1)
Next

