954,582 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Remove ending characters from string

Hello,
I'm writing code that will populate a listbox with the contents of a folder (a list of excel and word files). Naturally, the listbox has all file names with the ".xls" or ".doc" extension included. How can I get rid of this? I have tried

fileName.Remove()


and

fileName.Replace()


functions with no success. I can not specify the length of the file name, as they are all different and constantly changing. The only thing I know for sure is that the last four characters need removed, and then the remaining string to be put in the listbox. Any suggestions? Thank you in advance for any help!

bklynman01
Junior Poster in Training
94 posts since Oct 2010
Reputation Points: 12
Solved Threads: 6
 

Use a combination of:
LEFT function
String Length function

WaltP
Posting Sage w/ dash of thyme
Moderator
10,507 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

You can use the "Replace" function as in -

public Function StripOut(From as string, What as string) as string

    Dim i as Integer

    StripOut = From
    for i = 1 to len(What)
        StripOut = Replace(StripOut, mid$(What, i, 1), "")
    next i

End Function

'Just place it somewhere in your code (preferably in a module), and call it like this:

Text1.Text = StripOut("abcdefg", "bdf")
'This will return a string that had all of its 'b', 'd', and 'f' characters removed.


If you only want to remove the last say 3 characters or text, use the "RTrim" function -

Text1.Text = RTrim(Text1.Text,3)
'This will remove the last 3 characters.
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

Thanks Walt! I should have said it earlier, but I'm using VB.net 1.1 - I'm trying to upgrade! The code I ended up using is:

Me.strFile = Microsoft.VisualBasic.Left(strFile, strFile.Length - 4)

This will ensure that no matter what the extension is, it will not show in the box!

bklynman01
Junior Poster in Training
94 posts since Oct 2010
Reputation Points: 12
Solved Threads: 6
 

We all loose track once in a while. Glad your problem could be solved.

Happy coding:)

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 
Thanks Walt! I should have said it earlier, but I'm using VB.net 1.1


Then why did you post in the VB6 forum?The code I ended up using is:

Me.strFile = Microsoft.VisualBasic.Left(strFile, strFile.Length - 4)

This will ensure that no matter what the extension is, it will not show in the box!
Really? What if the file name is test.pl or trial.x ? :icon_wink:

WaltP
Posting Sage w/ dash of thyme
Moderator
10,507 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Hello, I'm writing code that will populate a listbox with the contents of a folder (a list of excel and word files). Naturally, the listbox has all file names with the ".xls" or ".doc" extension included. How can I get rid of this? I have tried

fileName.Remove()

and

fileName.Replace()
functions with no success. I can not specify the length of the file name, as they are all different and constantly changing. The only thing I know for sure is that the last four characters need removed, and then the remaining string to be put in the listbox. Any suggestions? Thank you in advance for any help!

Try using something like

FileName = Left$(FileName,LEn(FileName)-4)

This will change "Test.xls" to "Test".

JohnKelly
Light Poster
26 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
 

John, as you can see at the bottom, this thread was solved, which means the solution is above.:)

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You