开发者

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?


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


why not to use

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

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


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.


String lastArrayItem = _strFile[_strFile.Length-1];


You can always use regex

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


String str = "\\123\\abc\\hello\\.net\\winxp";
String[] tokens = str.Split('\\');
String lastToken = tokens[tokens.Length - 1];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜