zeichenfolge trennen beim letzten /

  • VB.NET

Es gibt 8 Antworten in diesem Thema. Der letzte Beitrag () ist von singu.

    zeichenfolge trennen beim letzten /

    Hallo,
    Ich habe das Problem. Ich will diesen Pfad

    C:\Users\Pascal\Music\Mika - Happy Ending.mp3

    trennen. Also in

    C:\Users\Pascal\Music\

    Mika - Happy Ending.mp3

    Nur ich habe keine Ahnung wie ich das anstellen soll.

    Bin dankbar für Hilfe.
    „Ex-ter-mi-nate all knock-knock jokes! They are an enemy of the daleks “ A Dalek
    Mein Blog zum Thema Klarträumen
    Dann suchst du in deinem String mit IntStrRev nach dem letzten Backslash, dann hast du die Position

    Beispiel:

    VB.NET-Quellcode

    1. If Pfad.Contains("\") = True Then
    2. Dim Slash As Integer = InStrRev(Pfad, "\") ' letzte Slash ermitteln
    3. Dateiname_Holen = Mid(Pfad, Slash + 1)
    4. Else
    5. Dateiname_Holen = Pfad
    6. End If


    du must natürlich den vorderen teil dann auslesen
    ich habe noch eine besseres Beispiel in meinen Programmen gefunden

    VB.NET-Quellcode

    1. Public Shared Function Pfad_Ohne_Datei(ByVal Pfad As String) As String
    2. 'filtert den Pfad aus einer Pfadangabe
    3. 'Beispiel: TextBox2.Text = "C:\System32\BS_Library.DLL"
    4. 'Ergebnis: TextBox1.Text = "C:\System32\"
    5. 'Aufrufbeispiel: TextBox2.Text = BS_Filter.Pfad_Ohne_Datei(TextBox1.Text)
    6. If String.IsNullOrEmpty(Trim(Pfad)) Then
    7. Pfad_Ohne_Datei = "Die Pfadangaben sind leer !"
    8. Exit Function
    9. Else : End If
    10. If Pfad.Contains("\") = True Then
    11. Dim Slash As Integer = InStrRev(Pfad, "\")
    12. Pfad_Ohne_Datei = Left(Pfad, Slash)
    13. Else
    14. Pfad_Ohne_Datei = "Es ist kein Backslash vorhanden!"
    15. End If
    16. End Function
    Oder so, bei folgendem Skript hast du die Infos auch gleich in einer List.

    VB.NET-Quellcode

    1. Public Class Form1
    2. Dim List As List(Of FileList)
    3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4. Dim Line As String = "C:\Users\Pascal\Music\Mika - Happy Ending.mp3"
    5. Dim File As String = Nothing
    6. Dim Path As String = Nothing
    7. Path = Line.Substring(0, Line.LastIndexOf(CChar("\")))
    8. File = Line.Substring(Line.LastIndexOf(CChar("\")) + 1, Line.Length - Line.LastIndexOf(CChar("\")) - 1)
    9. List.Add(New FileList(Path, File))
    10. MessageBox.Show(Path)
    11. MessageBox.Show(File)
    12. End Sub
    13. Public Class FileList
    14. Private propFile As String
    15. Private propPath As String
    16. Public Property File() As String
    17. Get
    18. Return propFile
    19. End Get
    20. Set(ByVal value As String)
    21. propFile = value
    22. End Set
    23. End Property
    24. Public Property Path() As String
    25. Get
    26. Return propPath
    27. End Get
    28. Set(ByVal value As String)
    29. propPath = value
    30. End Set
    31. End Property
    32. Public Sub New(ByVal Path As String, ByVal File As String)
    33. propFile = File
    34. propPath = Path
    35. End Sub
    36. End Class
    37. End Class