Hi I have an instance where I am trying to return part of a folder name. My application开发者_StackOverflow社区 is writtern in c#
My folder name returns this "Fri 11.4.97"
I only want to return the "11.4.97" part.
Any help would be greatly appreciated, thanks.
string folderName = "Fri 11.4.97"
string[] parts = folderName.Split(' ');
string lastPart = parts[parts.Length - 1];
Case in point: For such a simple case, a Regex might not be needed; and the above code might be more readable.
It depends on how well specified the input format is. If the file specification is always XXX blah blah blah
where XXX
is the bit you don't need (three characters for the day plus a space), you can just use a simple substring:
String dateBit = fspec.Substring (4);
Only if the file specification wasn't very "solid" would I consider using a regular expression. By that, I mean examples such as having the full day Friday
, or two spaces between the day and date. If you're always going to have a three-character date and single-character separator, the substring is probably the more natural choice.
\d+\.\d+\.\d+
will match three numeric fields, each separated by a period, without concern for the rest of the input.
精彩评论