我没安装 .NET的编程环境,给你一段VB6代码参考。 这个“语法”跟 VB.NET有些不同: 比如 Mid( )函数,你在 .NET中就可以用 “字符串.SubString”之类的代替。 具体细节我不清楚,反正在开发环境下都是“一看就知道”的。不同之处你自己修改。 Option ExplicitPrivate Sub Command1_Click() Dim strText As String Dim strTemp As String Dim strChar As String strTemp = Text1.Text strText = "" Do If ("" = strTemp) Then Exit Do strChar = Mid$(strTemp, 1, 1) strText = strText & strChar strTemp = Replace(strTemp, strChar, "") Loop Me.Cls Me.Print strText ' 你的作业中是赋值给某 Label End SubPrivate Sub Command2_Click() Dim strText As String Dim strChar As String Dim i&, p&, w As Long
strText = Text1.Text w = Len(strText) p = 0&: i = 1& Do If (i > w) Then Exit Do strChar = Mid$(strText, i, 1) For i = 1& + i To w If (strChar <> Mid$(strText, i, 1)) Then Exit For Next p = 1& + p Mid$(strText, p) = strChar Loop Me.Cls Me.Print Left$(strText, p) ' 你的作业中是赋值给某 Label End Sub
Private Sub Command1_Click() Label1 = Remove_repeated_characters(Text1.Text) End SubPrivate Sub Command2_Click() Label1 = Remove_continuous_characters(Text1.Text) End SubPrivate Sub Form_Load() Text1 = "FuDdan, fuuDdaaa, FFuuDDDaaa" End SubPrivate Function Remove_repeated_characters(ByVal strSource As String) As String Dim i As Long, j As Long, tmp As String, strResult As String strResult = Left(strSource, 1) For i = 2 To Len(strSource) tmp = Mid(strSource, i, 1)
If tmp = "," Or tmp = "" Then strResult = strResult & tmp Else For j = 1 To Len(strResult) If tmp = Mid(strResult, j, 1) Then Exit For Next j If j > Len(strResult) Then strResult = strResult & tmp End If Next i
Remove_repeated_characters = strResult End FunctionPrivate Function Remove_continuous_characters(ByVal strSource As String) As String Dim i As Long, strResult As String strResult = Left(strSource, 1) For i = 2 To Len(strSource) If Mid(strSource, i, 1) <> Mid(strSource, i - 1, 1) Then strResult = strResult & Mid(strSource, i, 1) Next i
Remove_continuous_characters = strResult End Function
第二个 得逐个字符比较了
这个“语法”跟 VB.NET有些不同:
比如 Mid( )函数,你在 .NET中就可以用 “字符串.SubString”之类的代替。
具体细节我不清楚,反正在开发环境下都是“一看就知道”的。不同之处你自己修改。
Option ExplicitPrivate Sub Command1_Click()
Dim strText As String
Dim strTemp As String
Dim strChar As String strTemp = Text1.Text
strText = ""
Do
If ("" = strTemp) Then Exit Do
strChar = Mid$(strTemp, 1, 1)
strText = strText & strChar
strTemp = Replace(strTemp, strChar, "")
Loop
Me.Cls
Me.Print strText ' 你的作业中是赋值给某 Label
End SubPrivate Sub Command2_Click()
Dim strText As String
Dim strChar As String
Dim i&, p&, w As Long
strText = Text1.Text
w = Len(strText)
p = 0&: i = 1&
Do
If (i > w) Then Exit Do
strChar = Mid$(strText, i, 1)
For i = 1& + i To w
If (strChar <> Mid$(strText, i, 1)) Then Exit For
Next
p = 1& + p
Mid$(strText, p) = strChar
Loop
Me.Cls
Me.Print Left$(strText, p) ' 你的作业中是赋值给某 Label
End Sub
改成了也是没有结果
改成了也是没有结果
不可能,除非你的 TextBox1 本来就是“空的”!另外,自己在 Do循环之前设置一个断点,然后单步跟踪运行一下,看看究竟是哪儿“不正确”啊。
改成了也是没有结果
不可能,除非你的 TextBox1 本来就是“空的”!另外,自己在 Do循环之前设置一个断点,然后单步跟踪运行一下,看看究竟是哪儿“不正确”啊。
这样是可以的,但是最后导出的结果,比如输入的是rrwwrrt,输出的是rwt
这样是可以的,但是最后导出的结果,比如输入的是rrwwrrt,输出的是rwt难道不应该是这个结果吗!
Label1 = Remove_repeated_characters(Text1.Text)
End SubPrivate Sub Command2_Click()
Label1 = Remove_continuous_characters(Text1.Text)
End SubPrivate Sub Form_Load()
Text1 = "FuDdan, fuuDdaaa, FFuuDDDaaa"
End SubPrivate Function Remove_repeated_characters(ByVal strSource As String) As String
Dim i As Long, j As Long, tmp As String, strResult As String strResult = Left(strSource, 1)
For i = 2 To Len(strSource)
tmp = Mid(strSource, i, 1)
If tmp = "," Or tmp = "" Then
strResult = strResult & tmp
Else
For j = 1 To Len(strResult)
If tmp = Mid(strResult, j, 1) Then Exit For
Next j
If j > Len(strResult) Then strResult = strResult & tmp
End If
Next i
Remove_repeated_characters = strResult
End FunctionPrivate Function Remove_continuous_characters(ByVal strSource As String) As String
Dim i As Long, strResult As String strResult = Left(strSource, 1)
For i = 2 To Len(strSource)
If Mid(strSource, i, 1) <> Mid(strSource, i - 1, 1) Then strResult = strResult & Mid(strSource, i, 1)
Next i
Remove_continuous_characters = strResult
End Function