Nur zahlen in einem text

  • VB.NET

Es gibt 9 Antworten in diesem Thema. Der letzte Beitrag () ist von diylab.

    Nur zahlen in einem text

    Hey, ich hab einen Text, der folgendermaßen aussieht:

    Max Mustermann 123456789

    Diesen wählt man in einem DropDown Menü aus.
    Jetzt möchte ich, dass nur die zahl in eine Deklaration kopiert wird.
    Sprich, ich klicke "Max Mustermann 123456789" im Dropdown an und in der Deklaration steht dann nur 123456789
    Wie mache ich das ?
    Besser wärs, du hättest richtige Datensätze. Also eine DataTAble mit den Spalten Name, Vorname, Nummer.

    Da könntest du in der Combo den Namen anzeigen, und in der Verarbeitung aber den ganzen Datensatz verwursten.

    Sind das alle Daten, die du zu verarbeiten hast, oder gibts da noch mehr? Weil ich bin ja schnell bei der Hand mit sich einarbeiten in relationale Datenverarbeitung - weil die ist auf sowas ausgelegt.
    Ich hab mich mit sowas leider noch nicht beschäftigt.
    Das sind die einzigen Datensätze ja :)
    Es gibt Vor und Nachnamen und eine ID
    und ich möchte da die ID rauswurschteln :)

    Hab jz folgende Notlösung gefunden, da die ID ja nur aus Zahlen besteht:

    ​TextBox1.Text = TextBox1.Text.Replace("a", "")
    TextBox1.Text = TextBox1.Text.Replace("b", "")
    TextBox1.Text = TextBox1.Text.Replace("c", "")
    TextBox1.Text = TextBox1.Text.Replace("d", "")
    TextBox1.Text = TextBox1.Text.Replace("e", "")
    TextBox1.Text = TextBox1.Text.Replace("f", "")
    TextBox1.Text = TextBox1.Text.Replace("g", "")
    TextBox1.Text = TextBox1.Text.Replace("h", "")
    TextBox1.Text = TextBox1.Text.Replace("i", "")
    TextBox1.Text = TextBox1.Text.Replace("j", "")
    TextBox1.Text = TextBox1.Text.Replace("k", "")
    TextBox1.Text = TextBox1.Text.Replace("l", "")
    TextBox1.Text = TextBox1.Text.Replace("m", "")
    TextBox1.Text = TextBox1.Text.Replace("n", "")
    TextBox1.Text = TextBox1.Text.Replace("o", "")
    TextBox1.Text = TextBox1.Text.Replace("p", "")
    TextBox1.Text = TextBox1.Text.Replace("q", "")
    TextBox1.Text = TextBox1.Text.Replace("r", "")
    TextBox1.Text = TextBox1.Text.Replace("s", "")
    TextBox1.Text = TextBox1.Text.Replace("t", "")
    TextBox1.Text = TextBox1.Text.Replace("u", "")
    TextBox1.Text = TextBox1.Text.Replace("v", "")
    TextBox1.Text = TextBox1.Text.Replace("w", "")
    TextBox1.Text = TextBox1.Text.Replace("x", "")
    TextBox1.Text = TextBox1.Text.Replace("y", "")
    TextBox1.Text = TextBox1.Text.Replace("z", "")
    TextBox1.Text = TextBox1.Text.Replace(" ", "")
    TextBox1.Text = TextBox1.Text.Replace("ä", "")
    TextBox1.Text = TextBox1.Text.Replace("ö", "")
    TextBox1.Text = TextBox1.Text.Replace("ü", "")

    TextBox1.Text = TextBox1.Text.Replace("A", "")
    TextBox1.Text = TextBox1.Text.Replace("B", "")
    TextBox1.Text = TextBox1.Text.Replace("C", "")
    TextBox1.Text = TextBox1.Text.Replace("D", "")
    TextBox1.Text = TextBox1.Text.Replace("E", "")
    TextBox1.Text = TextBox1.Text.Replace("F", "")
    TextBox1.Text = TextBox1.Text.Replace("G", "")
    TextBox1.Text = TextBox1.Text.Replace("H", "")
    TextBox1.Text = TextBox1.Text.Replace("I", "")
    TextBox1.Text = TextBox1.Text.Replace("J", "")
    TextBox1.Text = TextBox1.Text.Replace("K", "")
    TextBox1.Text = TextBox1.Text.Replace("L", "")
    TextBox1.Text = TextBox1.Text.Replace("M", "")
    TextBox1.Text = TextBox1.Text.Replace("N", "")
    TextBox1.Text = TextBox1.Text.Replace("O", "")
    TextBox1.Text = TextBox1.Text.Replace("P", "")
    TextBox1.Text = TextBox1.Text.Replace("Q", "")
    TextBox1.Text = TextBox1.Text.Replace("R", "")
    TextBox1.Text = TextBox1.Text.Replace("S", "")
    TextBox1.Text = TextBox1.Text.Replace("T", "")
    TextBox1.Text = TextBox1.Text.Replace("U", "")
    TextBox1.Text = TextBox1.Text.Replace("V", "")
    TextBox1.Text = TextBox1.Text.Replace("W", "")
    TextBox1.Text = TextBox1.Text.Replace("X", "")
    TextBox1.Text = TextBox1.Text.Replace("Y", "")
    TextBox1.Text = TextBox1.Text.Replace("Z", "")
    TextBox1.Text = TextBox1.Text.Replace("Ä", "")
    TextBox1.Text = TextBox1.Text.Replace("Ö", "")
    TextBox1.Text = TextBox1.Text.Replace("Ü", "")
    Hi,

    das ist Bullshit. Bau Dir ne ordentliche Datenverwaltung, mit der Du arbeiten kannst. Zumindest wenn Du es so machst, dann kannst Du beim Leerzeichen splitten und den letzten Teil des Arrays aufrufen. Ist sogar nen Einzeiler und somit viel kompakter:

    Visual Basic-Quellcode

    1. TextBox1.Text = "Max Mustermann 123556789".Split(" "c).Last()


    Den String musste halt anpassen.
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:
    Das ist ja grauenhafter Clusterfuck. Ist gibt eine IsNumeric Function für Strings, es gibt Regex und es gibt ein NumericUpDown Control. Außerdem gibt es Databindings, Datatables, typiserte DataSets (!), DataGridview....
    Hier mal ein Beispiel für ne saubere Anzeige eines Datensatzes in einer ComboBox.

    Visual Basic-Quellcode

    1. Private Class Person
    2. Public ReadOnly Property View() As String
    3. Get
    4. Return String.Format("{0} {1} {2}", vorname, nachname, number)
    5. End Get
    6. End Property
    7. Private vorname As String
    8. Private nachname As String
    9. Private number As Integer
    10. Public Sub New(vorname As String, nachname As String, number As Integer)
    11. Me.vorname = vorname
    12. Me.nachname = nachname
    13. Me.number = number
    14. End Sub
    15. End Class
    16. Private Sub Form1_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown
    17. Dim bl As New BindingList(Of Person)
    18. bl.Add(New Person("Max", "Mustermann", 1))
    19. bl.Add(New Person("Erika", "Mustermann", 2))
    20. ComboBox1.DataSource = bl
    21. ComboBox1.ValueMember = "View"
    22. End Sub


    Die BindingList könnte man wiederum an ein Datagridview binden oder noch besser: ein typisiertes Dataset verwenden.
    @diylab Also ersetzten würde ich da nicht. ​ MessageBox.Show(New System.Text.RegularExpressions.Regex("[0-9]+").Match("Max Mustermann 123456789").Value)
    Wäre ja auch irgendwie komisch. Ich lösche ja auch nicht alles bis auf das erste Wort, sondern hole mir das erste Wort direkt wenn ich es haben will :)
    Kla ist das ok, aber wir "erfahrenderen" sollten auf Sauberkeit achten. ;) (Okay das ist wirklich sehr pingelich)
    Btw. Sollte er ja sowieso ein anderes Datenmodell nutzen.

    Edit: @diylab Der war echt schlecht :p ^^

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „LaMiy“ ()