Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have a problem with trimming character from the start of a string. This is the code I have


VB
For Each i As String In lstSearch.Items

                Dim MyString As String = i

                Dim MyChar() As Char = {"\", "\", "1", "9", "2", ".", "1", "6", "8", ".", "6",, "\", "m", "y", "s", "h", "a", "r", "e", "_", "P", "r", "o", "d", "u", "c", "t", "i", "o", "n", "$", "\"}
                ' msgBox(MyChar)
                Dim newstring1 As String = MyString.TrimStart(MyChar)
                lstResults.Items.Add(newstring1)
            Next


Its part of searchng a directory for a file. The file is named something like this

AG000000_Share_Local_9_Reference.zip

But when I use the trim on it it always removes the A at the start of the file name. So it becomes

G000000_Share_Local_9_Reference.zip

I can't figure out why it keeps trimming the first character of the file name when it should only be trimming

\\192.168.6\myshare_production$\

but always trims

\\192.168.6\myshare_production$\A

I'd be greatful if someone could help

Thanks
J
Posted

What did you think the first sentence means in MSDNs documentation of String.TrimStart[^].

"Removes all leading occurrences of a set of characters specified in an array from the current String object."

This happens case insenstive so the a is also removed.

BTW this is a very poor way to split a file name off the path. Please try like this instead:

C#
String fileName = @"\\192.168.6.1\myshare_production$\AG000000_Share_Local_9_Reference.zip";
String path     = @"\\192.168.6.1\myshare_production$\";
String result   = null;

result = Path.GetFileName(fileName);
Console.WriteLine("GetFileName('{0}') returns '{1}'",fileName, result);

result = Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'", path, result);

Regards,

Manfred
 
Share this answer
 
v2
Comments
VJ Reddy 21-May-12 7:58am    
Good answer. 5!
When I'm searching for the file the results are put into one list box. I then am trying to populate the second list box with just the filename. I'm using

lstSearch.Items.AddRange(Directory.GetFiles(path, part, SearchOption.TopDirectoryOnly))


to return all the filenames into the lstSearch list box. This is handy as I can search for only a part of the filename as I have mutiple files that start with the same few characters. I just don't want the full path to display in the list box wheich is what the line of code is doing above.
 
Share this answer
 
I fixed it using

VB
For Each i As String In lstSearch.Items

              Dim MyString As String = i

              file1 = MyString.Substring(MyString.LastIndexOf("\") + 1)
              lstResults.Items.Add(file1)
          Next

seems to works as I want it to now
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900