Edw*_*uay 0 c# string refactoring
我怎样才能重构这一点,以便numberOfItems不必被声明为变量?
//method: gets the text in a string in front of a marker, if marker is not there, then return empty string
//example: GetTextAfterMarker("documents/jan/letter043.doc","/") returns "letter043.doc"
//example: GetTextAfterMarker("letter043.doc","/") returns ""
//example: GetTextAfterMarker("letter043.doc",".") returns "doc"
public static string GetTextAfterMarker(string line, string marker)
{
int numberOfItems = line.Split(new string[] { marker }, StringSplitOptions.None).Count();
string result = line.Split(new string[] { marker }, StringSplitOptions.None)[numberOfItems-1];
return line.Equals(result) ? string.Empty : result;
}
Run Code Online (Sandbox Code Playgroud)
var index = line.LastIndexOf (marker) ;
return index < 0 ? string.Empty : line.Substring (index + marker.Length) ;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
345 次 |
| 最近记录: |