is there some sort of "end of string" method

j00sh

Active member
Joined
Mar 29, 2007
Messages
35
Programming Experience
Beginner
I want to do a test on whether a string's last character is "\"

keep in mind I only want to strip off the last \ not all of them
 
Last edited:
guess I just had to wake up and start thinking straight :D

VB.NET:
        Dim intLength As Integer
        intLength = Microsoft.VisualBasic.Len(strPath)
        Dim strEnd As String = Microsoft.VisualBasic.Right(strPath, 1)
        If strEnd = "\" Then
            strPath = Microsoft.VisualBasic.Left(strPath, intLength - 1)
        End If
 
you could do it this way:
VB.NET:
If strPath.EndsWith(System.IO.Path.DirectorySeperatorChar) = True Then strPath = strPath.SubString(0, strPath.Length - 2)
 
I want to do a test on whether a string's last character is "\"

keep in mind I only want to strip off the last \ not all of them

Actually, the easiest way is not to care whether it is there or not:

Dim stripped as String = original.TrimEnd("\"c)


Whether or not your original has a slash, it now doesnt.

Do note though that if youre doing this to prevent a path forming like:

c:\temp\\somefile.txt

you need to know that windows doesnt actually care how many slashes appear in a row. The following are equivalent:

c:\\\\\temp\\\\\\\\\\\some.txt
c:\temp\some.txt
 
untrue :p

but thanks for that tip
 

Attachments

  • 2slashes.PNG
    2slashes.PNG
    103.3 KB · Views: 40
you dont use backslashes in URLs; theyre illegal characters! :rolleyes::rolleyes:

Click Start
Click Run
Type C:\\\\\\\\\\\\\\\\windows
Watch it not complain
 
you dont use backslashes in URLs; theyre illegal characters! :rolleyes::rolleyes:

Click Start
Click Run
Type C:\\\\\\\\\\\\\\\\windows
Watch it not complain

that's because MS has code that removes all the extra slashes so that it would not cause a problem

as far as i know, in VB.Net you need to code for the removing of those extra slashes yourself because (as j00sh pointed out) those extra slashes causes errors

now a way to remove all the extra slashes would be to write a quick function that takes the current path (with the extra slashes) then split that string into an array with the "\" being the split char, then simply reassemble the path only inserting 1 "\" between each piece then return that new path

by doing it this way:
strPath= YourRemoveSlashes("C:\\\\Windows\\\\\\System")

now strPath="C:\Windows\System" 'this is error free unless windows is not installed on drive C:
 
Both Framework 1.1 and 2.0 handles all these backslashes here - doesn't yours?
 
I was gonna say, when doing things like making a FileReader or something:


Dim fi as FileInfo("c:\\\\\temp\\\\\whatever.txt")
Dim sr as StreamReader = fi.OpenText()

My code never has any problems.. I regularly just take whatever the user provided, add a slash and use it regardless:

Dim fi as FileInfo(locationTextBox.Text & "\" & filenameTextBox.Text)
Dim sr as StreamReader = fi.OpenText()

I am now, however, thanks to JohnH pointing out a useful thing recently:

Dim fi as FileInfo(IO.Path.Combine(locationTextBox.Text, filenameTextBox.Text))
Dim sr as StreamReader = fi.OpenText()

-
And accessory to this discussion, \ should never be used in URLs..

Path:
c:\temp\whatever.txt

Equivalent url:
file://c:/temp/whatever.txt

Of course, because URLs DO care about extra slashes, the following URL is invalid:
file://c://///temp/////whatever.txt

!
 
i had not had the time to test that for VS 2003 and VS 2005 until now, of which the extra slashes in the path doesn't bother .Net in any ways either, I know with vb6 it used to be a huge deal

my bad
 
Back
Top