Search code examples
c#.netstringmaxlength

how to find the max index of a string from variable in c#?


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?


Solution

  • 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);