Hi, I just want to know if i can remove a part of a string
Exemple : in Textbox1 i have "C:\Users\Someone\Desktop\test.exe" and i want delete the string until the "\" so in the textbox i get "C:\Users\Someone\Desktop\"
Thanks if you reply me and sorry for the bad english, its not my main language ...

Dani AI

Generated

Short answer: yes — in VB.NET you can remove the file name from a full path. was right that a LastIndexOf + Substring approach works; pointed to SubString docs. For robustness, use the framework helper in System.IO.Path (recommended), and only fall back to manual substring when you need very specific behavior.

Recommended (robust) approach — use Path.GetDirectoryName, then append a trailing backslash if you want it included:

Dim original As String = TextBox1.Text
If Not String.IsNullOrWhiteSpace(original) Then
    Dim dir As String = System.IO.Path.GetDirectoryName(original)
    If String.IsNullOrEmpty(dir) Then
        ' filename-only or special cases: try to keep the root if present
        dir = System.IO.Path.GetPathRoot(original)
    End If
    If Not String.IsNullOrEmpty(dir) AndAlso Not dir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
        dir &= System.IO.Path.DirectorySeparatorChar
    End If
    TextBox1.Text = dir
End If

Simpler/manual method (what described) — find the last "\" and take a substring. This is straightforward but you must guard for no-backslash or root-only inputs:

Dim s As String = TextBox1.Text
Dim idx As Integer = s.LastIndexOf("\"c)
If idx >= 0 Then
    TextBox1.Text = s.Substring(0, idx + 1) ' include trailing backslash
End If

Notes and pitfalls: normalize or validate input if it might be a relative path, a UNC path (\server\share...), or already end with a slash. Path.GetDirectoryName handles many cases for you; manual string ops are fine for quick scripts but require extra checks.

Recommended Answers

All 4 Replies

You haven't mentioned what language you intend to be using for this but the answer is yes, it's possible. Depending on what functions you have available in your chosen language you need to determine the location of the last \ and then create a substring from position 0 to the that last '\'.
A lot of languages have methods that will provide you with the file name, the thing.exe part of a path as well, which would allow to do a replace, swapping the file name with an empty string. Or alternatively return the path with the file name removed automatically.
If we knew what language you were using we could provide actual examples.

oh sorry i use vb.net

Have a look at the SubString method

Ok thanks you all for the help i found what i search :) !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.