从 vb.net 中的字符串中提取字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13245530/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:02:38  来源:igfitidea点击:

Extracting characters from a string in vb.net

vb.netstringcharacterextract

提问by Timothy Coetzee

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.

我正忙着写过去的试卷,为下周的 vb.net 考试做准备。我正在努力解决的问题如下。

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

取一长串字母字符和特殊字符,将字母字符提取到string1,将特殊字符提取到string2

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

所以字符串 hello://thisismystring!? 必须显示如下

string1 = hellothisismystring
string2 = ://!?

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

我的问题是如何从字符串中提取字符并将它们存储在变量中?

回答by AMissico

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

一种对 Unicode 友好、干净且简单的方法。

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

回答by Samuel Adam

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.

您可以遍历该字符串中的字符,并使用 ASCII 代码来确定当前字符是否为字母表,如果是 -> string1,如果不是 -> string2。祝你好运。

回答by Ahmad

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)

回答by Christian Sauer

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....

您必须将所有所需的字符写入“ToFind” 正则表达式会更好地工作,但它们有时很麻烦....

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