views:

55

answers:

6

suppose i have a string as

        string[] _strFile;

        foreach (ListViewItem item in listview1.Items)
        {
            string _strRecFileName = item.SubItems[5].Text;
            _strFile = _strRecFileName.Split('\\');
        }

in my listview i have a string as \123\abc\hello\.net\*winxp* now i want to get the last value of the string i.e. winxp in this case.. what is the function to do that?

can i use getupperbond function to calculate the upper bound of the string and how to use it?

+2  A: 
string[] files = _strRecFileName.Split('\\');
string lastElement = files[files.Length - 1];

Of course, if you're dealing with actual filenames and paths and stuff, it's probably easier to just use the Path class:

string fileName = Path.GetFileName(_strRecFileName);
Dean Harding
A: 
String str = "\\123\\abc\\hello\\.net\\winxp";
String[] tokens = str.Split('\\');
String lastToken = tokens[tokens.Length - 1];
Scott Smith
+2  A: 

why not to use

string expectedPart = Path.GetFileName(item.SubItems[5].Text);

http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx

zerkms
+1  A: 

try this:

string s = @"a\b\c\d\e";
int index = s.LastIndexOf('\\');
string fileName = s.Substring(index + 1);

You will only create one extra string in this case so it will use less memory than an array of strings. But as codeka said if the text is a proper path then the Path class would be better.

Cornelius
A: 
String lastArrayItem = _strFile[_strFile.Length-1];
Veer
A: 

You can always use regex

string sub = System.Text.Regularexpressions.Regex.Match(TextVar,@"\\(\w+?)$").groups[1].value
rerun