by
Roland Schumacher alias GENiALi
17. April 2008 -- 129
words -- 2091 mal gelesen
Die Frage war:
You are writing an application that processes phone numbers users have typed as strings. Which of the following code samples would reformat any of the following inputs?
555-555-1111
(555) 555-1111
5555551111
555 555-1111
In the following code samples, the string input is passed to the method as s.
// C#
static string ReformatPhone(string s)
{
Match m = Regex.Match(s, @"^\(?(\d{3})\)?[\s\-]?(\d{3})\-?(\d{4})$");
return String.Format("({0}) {1}-{2}", m.Groups[1], m.Groups[2], m.Groups[3]);
}
Wo ist hier der Unterschied? Das sind zwei Antworten. Aber wir finden
den Unterschied nicht. Wer findet ihn? :-)
// C#
static string ReformatPhone(string s)
{
Match m = Regex.Match(s, @"^\(?(\d{3})\)?[\s\-]?(\d{3})\-?(\d{4})$");
return String.Format("({0}) {1}-{2}", m.Groups[1], m.Groups[2], m.Groups[3]);
}
Richtig soll diese Antwort sein.
// C#
static string ReformatPhone(string s)
{
Match m = Regex.Match(s, @"^\(?(\d{3})\)?[\s\-]?(\d{3})\-?(\d{4})$");
return String.Format("({0}) {1}-{2}", m.Groups[1], m.Groups[2], m.Groups[3]);
}