面试题目记录
面试题目1: 把一个段落中的日期格式字符串更改格式,由MM/DD/YYYY 更改为 DD/MM/YYYY
比如 输入语句为 "Last time it rained was on 07/25/2013 and today is 08/09/2013" 转换后的输出语句为 "Last time it rained was on 25/07/2013 and today is 09/08/2013"
方法: 采用正则表达式,把段落中的日期格式字符串替换
static void Main(string[] args) { string newStr = UpdateDateFormat("Last time it rained was on 07/25/2013 and today is 08/09/2013"); Console.WriteLine(newStr); } public static string UpdateDateFormat(string a) { string[] str1 = a.Split(' '); var pattern = @"(\d+)/(\d+)/(\d+)"; Regex rgx = new Regex(pattern, RegexOptions.None,TimeSpan.FromMilliseconds(150)); foreach (var item in str1) { var newItem = ""; if (item.IndexOf('/') > 0) { //a = a.Replace(item, Convert.ToDateTime(item).ToString("dd/MM/yyyy")); newItem = Regex.Replace(item, pattern, "$2/$1/$3", RegexOptions.None, TimeSpan.FromMilliseconds(150)); a = a.Replace(item, newItem); } } return a; }
2. 判断一条语句是否是 palindrome (回文)
public static bool IsPalindrome(string s) { string newS = s.Replace(" ", "").ToLower(); //Get the first and last index of the string int beginIndex = 0; int endIndex = newS.Length - 1; //Compare and judge whether the char is same from begin and end direction while (beginIndex < endIndex) { if (newS[beginIndex] != newS[endIndex]) return false; beginIndex++; endIndex--; } return true; }