string projectname= "1.25hr projectA";
如何解析上面的字符串值以删除时间字符串。输入值可以使用格式1hr projectA
或1.25hr projectA
,输出将为projectA
。
projectname = projectname.Substring(0, projectname.IndexOf('r')).Trim();
在这一行中输出像1.25h。
char[] delimiterChars = { 'r' };
string projectname = lstbxindex.SelectedItem.ToString();
string[] words = projectname.Split(delimiterChars);
projectname = projectname.Substring(0, projectname.IndexOf('r')).Trim();
答案 0 :(得分:3)
你的问题是模糊的。如果你想删除 /scripts/migrate_ea3_to_ea4
等类似的死亡:
"1.25hr "
如果你想修剪并包括最后“hr”
string source = "my 1.45hr and 5.31hr test project";
// "my and test project"
string result = Regex.Replace(source, @"[0-9.]+hr\s+", "");
答案 1 :(得分:1)
您可以使用简单的正则表达式解析文本:Regex.Replace(rawValue, @"^[\d|.|hr]+", "").Trim()
[Theory]
[InlineData("1.25hr projectA")]
[InlineData("1hr projectA")]
[InlineData("projectA")]
public void Can_parse_project_name(string rawValue)
{
var projectName= Regex.Replace(rawValue, @"^[\d|.|hr]+", "").Trim();
projectName.Should().Be("projectA");
}