面试题目记录

面试题目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;

        }
复制代码

 

posted on   新西兰程序员  阅读(337)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示