C#,.NET 3.5解析C#中的字符串;有沒有更清潔的方法?
這對我來說只是一種醜陋的氣味,但我想不出另一種方式。
給定一個格式爲「Joe Smith(jsmith)」(無引號)的字符串,我想解析出括號內的'jsmith'字符串。我想出了這一點:
private static string DecipherUserName(string user)
{
if(!user.Contains("("))
return user;
int start = user.IndexOf("(");
return user.Substring(start).Replace("(", string.Empty).Replace(")", string.Empty);
}
其他比我(UN)健康厭惡正則表達式,有一個簡單的解析出子呢?爲了澄清,要解析的字符串總是:「Joe Smith(jsmith)」(sans quotes)。
忘記IndexOf將返回-1時,該值不存在。謝謝! – 2009-04-08 02:38:15