Results 1 to 9 of 9

Thread: Trim a string

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    205

    Trim a string

    I like to trim the following string:
    "c:\program file\MTC\MTC\BIN\DEBUG"
    into
    "c:\program file\MTC"

    What is the best way to do this? Thanks!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Trim a string

    Use the IO.Path class to manipulate file and folder paths. IO.Path.GetDirectoryName will trim from the last slash from a path. Call GetDirectoryName three times on your path and you'll get the desired result. Of course, how exactly you would know to call it three times is up to you. How do you know now that you want to remove the lowest three folders?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Trim a string

    This might also work but wouldn't be anywhere as robust as the previously suggested method. This is more my level:


    Code:
            Dim str As String = "c:\program file\MTC\MTC\BIN\DEBUG"
            Dim cnt, i As Integer
    
            For Each chr As Char In str
                i += 1
                If chr = "\" Then cnt += 1
                If cnt = 3 Then
                    str = str.Substring(0, i - 1)
                    Exit For
                End If
            Next
    
            MessageBox.Show(str)
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    205

    Re: Trim a string

    Thank you both.

    Well the story behide this question is that I need to find out what directory user installed my app to. In my app I need to referrence some sub-directories under the application folder. The only way I know how is to use Application.StartupPath. But Application.StartupPath returns {Application folder}\{Product Name}\bin\debug. In the above case, it's c:\Program File\MTC\MTC\bin\debug. Since I am interested in only c:\Program File\MTC, so I need to trim the rest of the string.

    Is there any easier way to get the application folder, so I don't need to go through the trimming? If there is, can you please let me know? Thanks!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Trim a string

    Application.StartupPath returns the path to the folder containing the current executable. It will obvioulsy return the bin\Debug path while debugging because that's where the executable is located. You shouldn't have a bin\Debug folder when you're running it on a machine on which it's been installed though. If you do then you've messed up your installer.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Trim a string

    It is giving you that long path (\bin\debug) because that is where the .exe is created when you are developing the program. After you are done, assuming you have some sort of installation package or whatever, the pathname will not have those things. So, if chopping off the last three folders works now, it probably won't work later.

    What is you application is installed in C:\ProgramFiles\MTC? This will no longer work because there aren't even three folders to chop.

    Of course, how exactly you would know to call it three times is up to you. How do you know now that you want to remove the lowest three folders?
    That is what jmc was getting at with that statement.

    They way you are trying to solve this problem will only work for the moment. You need to think of a better way to get to the folder you want.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    205

    Re: Trim a string

    So what you guys are saying is that when I do a release build, and if user select to install the application in c:\Program File\MTC, Application.StartupPath will return c:\Program File\MTC, and NOT c:\Program File\MTC\MTC\bin\release? I am sorry, I am a little confused.

  8. #8
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Trim a string

    Exactly, or maybe MTC\MTC, I am not certain. The point is, it will change. Its called Application.StartupPath. So, it changes when the application gets moved.

    Do, this. Create a program, add a button. Have the button do this
    Code:
    msgbox(Application.StartupPath)
    That will just put Application.StartupPath in a message box. Now, run it and you should have some sort of \bin\debug in it. Now go into bin\debug (in windows explorer, assuming you are using windows) and copy the .exe to your desktop and run it. You should see a difference. It should change depending on where you are running the exe.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    205

    Re: Trim a string

    Oh, OK, I tried what you suggested. I have a better understanding now. Thank you very much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width