VB中如何将String中的数字与字母提取出来?

hotjo110 2013-05-03 02:44:12
VB中如何将String中的数字与字母提取出来? 用function来做,必须包含while。


Private Function changeToUp(ByVal inputdata) As String
Dim input As String
inputdata = txtTest.Text.Trim
If inputdata.Length <= 0 Then
input = "Note :Please enter a string with letters or digits"
Else
'convert string's letters to upper case
input = inputdata.ToUpper

Dim intLastPos = input.Length - 1
Dim intPos As Integer = 0
Do

Dim chrVar As Char = input.Chars(intPos)
If chrVar >= "A" And chrVar <= "Z" Then
inputdata = input & chrVar
ElseIf input(intPos).ToString.Equals("0") Then
inputdata = inputdata & "0"
ElseIf input(intPos).ToString.Equals("1") Then
inputdata = inputdata & "1"
ElseIf input(intPos).ToString.Equals("2") Then
inputdata = inputdata & "2"
ElseIf input(intPos).ToString.Equals("3") Then
inputdata = inputdata & "3"
ElseIf input(intPos).ToString.Equals("4") Then
inputdata = inputdata & "4"
ElseIf input(intPos).ToString.Equals("5") Then
inputdata = inputdata & "5"
ElseIf input(intPos).ToString.Equals("6") Then
inputdata = inputdata & "6"
ElseIf input(intPos).ToString.Equals("7") Then
inputdata = inputdata & "7"
ElseIf input(intPos).ToString.Equals("8") Then
inputdata = inputdata & "8"
ElseIf input(intPos).ToString.Equals("9") Then
inputdata = inputdata & "9"

Else
End If
intPos += 1
Loop Until intPos >= intLastPos


End If
Return inputdata
End Function




这样的取值当输入标点符号的时候,产生问题。
...全文
835 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
xc_aspnet 2013-05-20
  • 打赏
  • 举报
回复
循环一个个的读取判断
lliai 2013-05-20
  • 打赏
  • 举报
回复
一个一个字符处理。
deephill 2013-05-19
  • 打赏
  • 举报
回复
用正则表达式确实容易
无·法 2013-05-06
  • 打赏
  • 举报
回复
费事,用正则表达式“[a-zA-Z0-9]”,几行代码而已。
bcrun 2013-05-05
  • 打赏
  • 举报
回复
不过你表述的还不够清楚,如果有个调用示例什么的则更好。
hotjo110 2013-05-04
  • 打赏
  • 举报
回复
引用 2 楼 sysdzw 的回复:
散分的么?谢谢分享
当然!
hotjo110 2013-05-04
  • 打赏
  • 举报
回复
Public Class FormPalindromeRecognizer

Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtTest.Text = String.Empty
lblAbout.Text = String.Empty
txtTest.ReadOnly = False
btnClear.Enabled = False
btnTest.Enabled = True
txtTest.Focus()
End Sub

Private Sub btnTest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTest.Click
Dim strAbout As String = String.Empty
'Dim strTest As String
Dim strEdited As String

Try
strEdited = removeAllButLettersAndDigits(txtTest.Text)

If strEdited.Length <= 0 Then
strAbout = "Please enter a string with some letters"
Else
'convert string's letters to upper case
'strEdited = removeAllButLettersAndDigits(txtTest.Text)

Dim strReversed As String = String.Empty

strReversed = reverseString(strEdited)

If strEdited = strReversed Then
strAbout = "It is a palindrome"
Else
strAbout = "It is not a palindrome"
End If
End If
'End If

lblAbout.Text = strAbout
txtTest.ReadOnly = True
btnClear.Enabled = True

btnTest.Enabled = False
' End If
Catch ex As Exception
MessageBox.Show("Please enter a string")
End Try

End Sub

Public Function removeAllButLettersAndDigits(ByVal changeText As String) As String




changeText = changeText.ToUpper

'filter out non-alphabetic characters
Dim strEdited As String = String.Empty
Dim into As String
Dim intLastPos As Integer = changeText.Length - 1
Dim x = 0
Do Until x = intLastPos
into = changeText.Substring(x, x + 1)

If into >= "A" And into <= "Z" Then
strEdited = strEdited & into
x += 1
ElseIf Int(into) >= 0 And Int(into) <= 9 Then
strEdited = strEdited & into
x += 1
End If
Loop
Return strEdited
'For intPos As Integer = 0 To intLastPos
' Dim chrVar As Char = changeText.Chars(intPos)
' If chrVar >= "A" And chrVar <= "Z" Then
' strEdited = strEdited & chrVar
' End If
'Next

'Dim i As Long
'changeText = changeText.ToUpper
'' Ignore everything except letters and numbers
'i = 1
'Do
' Select Case Mid(changeText, i, 1)
' Case "a" To "z", "0" To "9" : i = i + 1
' Case Else : changeText = changeText & changeText.Substring(Mid(i, 1))
' End Select
'Loop While i <= Len(changeText)

'Return changeText

End Function

Public Function reverseString(ByVal convertText As String) As String
Dim max As Integer

Dim TextValue As String = String.Empty
max = convertText.Length - 1
Dim output As String
Do Until max <= 0
output = convertText.Substring(max, max + 1)

TextValue = output & TextValue
max -= 1
Loop

Return TextValue
End Function

End Class




现在的思路
hotjo110 2013-05-04
  • 打赏
  • 举报
回复
引用 3 楼 bcrun 的回复:
有人散分就有人接分。楼主贴的代码竟然有大段被注释掉的,呵呵 。
没有办法啊,思路反复自我否定,到现在都没有搞清楚!
bcrun 2013-05-04
  • 打赏
  • 举报
回复
有人散分就有人接分。楼主贴的代码竟然有大段被注释掉的,呵呵 。
无·法 2013-05-04
  • 打赏
  • 举报
回复
散分的么?谢谢分享
hotjo110 2013-05-03
  • 打赏
  • 举报
回复
Public Class FormPalindromeRecognizer

Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtTest.Text = String.Empty
lblAbout.Text = String.Empty
txtTest.ReadOnly = False

btnClear.Enabled = False
btnTest.Enabled = True

txtTest.Focus()
End Sub
Private Function changeToUp(ByVal inputdata) As String
Dim input As String
inputdata = txtTest.Text.Trim
If inputdata.Length <= 0 Then 'Try statements
input = "Note :Please enter a string with letters or digits"
Else
'convert string's letters to upper case
input = inputdata.ToUpper

Dim intLastPos = input.Length - 1
Dim intPos As Integer = 0
Do

Dim chrVar As Char = input.Chars(intPos)
If chrVar >= "A" And chrVar <= "Z" Then
inputdata = input & chrVar

'ElseIf Int(input(intPos)) >= 0 And Int(input(intPos)) <= 9 Then
' input = input & Int(input(intPos))
'End If
ElseIf input(intPos).ToString.Equals("0") Then
inputdata = inputdata & "0"
ElseIf input(intPos).ToString.Equals("1") Then
inputdata = inputdata & "1"
ElseIf input(intPos).ToString.Equals("2") Then
inputdata = inputdata & "2"
ElseIf input(intPos).ToString.Equals("3") Then
inputdata = inputdata & "3"
ElseIf input(intPos).ToString.Equals("4") Then
inputdata = inputdata & "4"
ElseIf input(intPos).ToString.Equals("5") Then
inputdata = inputdata & "5"
ElseIf input(intPos).ToString.Equals("6") Then
inputdata = inputdata & "6"
ElseIf input(intPos).ToString.Equals("7") Then
inputdata = inputdata & "7"
ElseIf input(intPos).ToString.Equals("8") Then
inputdata = inputdata & "8"
ElseIf input(intPos).ToString.Equals("9") Then
inputdata = inputdata & "9"

Else
End If
intPos += 1
Loop Until intPos >= intLastPos


End If
Return inputdata
End Function

Private Sub btnTest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTest.Click
Dim strAbout As String = String.Empty
Dim strTest As String
Dim strEdited As String
Dim intLastPos As Integer
'input

'strTest = changeToUp(txtTest.Text)

'strEdited = strTest
'If strTest.Length <= 0 Then 'Try statements
' strAbout = "Please enter a string with some letters"
'Else
' 'convert string's letters to upper case
' strTest = strTest.ToUpper
'Try



strEdited = changeToUp(txtTest.Text)

' strEdited = strTest
'strTest = txtTest.Text
'strTest = strTest.Trim
'strTest = strTest.ToUpper
'strEdited = String.Empty
' intLastPos = strTest.Length - 1


'strEdited = filterOutLettle(strEdited)
'For intPos As Integer = 0 To intLastPos
' Dim chrVar As Char = strTest.Chars(intPos)
' If chrVar >= "A" And chrVar <= "Z" Then
' strEdited = strEdited & chrVar

' ElseIf (Int(strTest(intPos).ToString) >= 0) And (Int(strTest(intPos).ToString) <= 9) Then
' strEdited = strEdited & strTest(intPos).ToString
' Else

' End If
' Next
'ElseIf strEdited(intPos).ToString.Equals("0") Then
' strEdited = strEdited & "0"
'ElseIf strEdited(intPos).ToString.Equals("1") Then
' strEdited = strEdited & "1"
'ElseIf strEdited(intPos).ToString.Equals("2") Then
' strEdited = strEdited & "2"
'ElseIf strEdited(intPos).ToString.Equals("3") Then
' strEdited = strEdited & "3"
'ElseIf strEdited(intPos).ToString.Equals("4") Then
' strEdited = strEdited & "4"
'ElseIf strEdited(intPos).ToString.Equals("5") Then
' strEdited = strEdited & "5"
'ElseIf strEdited(intPos).ToString.Equals("6") Then
' strEdited = strEdited & "6"
'ElseIf strEdited(intPos).ToString.Equals("7") Then
' strEdited = strEdited & "7"
'ElseIf strEdited(intPos).ToString.Equals("8") Then
' strEdited = strEdited & "8"
'ElseIf strEdited(intPos).ToString.Equals("9") Then
' strEdited = strEdited & "9"

'Else


'Catch ex As FormatException
'strAbout = "Please enter a string with some letters~~~!"
' End Try


'filter out non-alphabetic characters
'Dim strEdited As String = String.Empty
' strEdited = filterOutLettle(strTest.Length - 1)
'Dim intLastPos As Integer = strTest.Length - 1
'For intPos As Integer = 0 To intLastPos
' Dim chrVar As Char = strTest.Chars(intPos)
' If chrVar >= "A" And chrVar <= "Z" Then
' strEdited = strEdited & chrVar
' End If
'Next



If strEdited.Length <= 0 Then
strAbout = "Please enter a string that contains letters or digits."
Else
'build reversed - the reverse of edited string


Dim strReversed As String = String.Empty
intLastPos = strEdited.Length - 1
For intPos As Integer = 0 To intLastPos
Dim chrVar As Char = strEdited.Chars(intPos)
strReversed = chrVar & strReversed
Next

If strEdited = strReversed Then
strAbout = "It is a palindrome"
Else
strAbout = "It is not a palindrome"
End If
End If
'End If

lblAbout.Text = strAbout
txtTest.ReadOnly = True
btnClear.Enabled = True

btnTest.Enabled = False
End Sub
'Private Function filterOutLettle(ByVal enterdata As String) As String


' Dim intLastPos = enterdata.Length - 1
' For intPos As Integer = 0 To intLastPos
' Dim chrVar As Char = enterdata.Chars(intPos)
' If chrVar >= "A" And chrVar <= "Z" Then
' enterdata = enterdata & chrVar

' ElseIf (Int(enterdata(intPos).ToString) >= 0) And (Int(enterdata(intPos).ToString) <= 9) Then
' enterdata = enterdata & enterdata(intPos).ToString
' Else

' End If
' Next
' Return enterdata
'End Function
End Class

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧