Extracting characters from a string in vb.net

*爱你&永不变心* 提交于 2019-12-25 02:18:51

问题


I am busy working on past exam papers in preparation for my vb.net exam next week. The question I am struggling with is as follows.

Take in a long string of alphabetical characters and special characters extract the alphabetical characters to string1 and extract special characters to string2

so the string hello://thisismystring!? must be displayed as follows

string1 = hellothisismystring
string2 = ://!?

My question is this how do I extract characters from a string and store them in a variable?


回答1:


One way that is Unicode-friendly, clean and easy.

Imports System.Text

Module Module1

    Sub Main()
        Dim sValue As String = "hello://thisismystring!?"
        Dim a As New StringBuilder
        Dim b As New StringBuilder
        For Each c As Char In sValue
            If Char.IsLetter(c) Then
                a.Append(c)
            Else
                b.Append(c)
            End If
        Next
        Dim s1 As String = a.ToString()
        Dim s2 As String = b.ToString()
    End Sub

End Module



回答2:


You could loop through the characters in that string, and use ASCII code to determine if the current character is alphabet, if yes -> string1, if no -> string2. Good luck.




回答3:


Here is one way to do it

    Dim allText As String = "Hello139874098@#4this204985"
    Dim onlyLetters As String = ""
    Dim nonLetters As String = ""
    For i = 0 To allText.Length - 1
        Dim c As String = allText.Substring(i, 1)
        If c.ToUpper >= "A" And c.ToUpper <= "Z" Then
            onlyLetters &= c
        Else
            nonLetters &= c
        End If
    Next
    MsgBox("Letters are " & onlyLetters)
    MsgBox("Non-Letters are " & nonLetters)



回答4:


I would use this:

    Dim SearchString = "hello://thisismystring!?"
    Dim ToFind = "://!?"


    Dim ResultSpecial As String = ""
    Dim ResultNormal As String = ""

    Dim ChrList As New List(Of Char)

    For Each c In ToFind
        ChrList.Add(c)
    Next

    For i = 0 To SearchString.Length - 1
        Dim c = SearchString(i)
        If ChrList.Contains(c) = True Then
            ResultSpecial = ResultSpecial & c
        Else
            ResultNormal = ResultNormal & c
        End If
    Next

    Debug.Print("Resultnormal: " & ResultNormal)
    Debug.Print("ResultSpecial: " & ResultSpecial)

You have to write all desired Character into "ToFind" A regex would work even better, but they are sometimes troublesome....

Reworked Version

Module Module1

    Sub Main()
        Dim SearchString = "hello://thisismystring!?"
        Dim ToFind = "://!?"

        Dim ResultSpecial As String = ""
        Dim ResultNormal As String = ""

        For Each c In SearchString
            If ToFind.Contains(c) Then
                ResultSpecial = ResultSpecial + c
            Else
                ResultNormal = ResultNormal + c
            End If
        Next

        Debug.WriteLine(ResultNormal, "Resultnormal")
        Debug.WriteLine(ResultSpecial, "ResultSpecial")
    End Sub

End Module


来源:https://stackoverflow.com/questions/13245530/extracting-characters-from-a-string-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!