输入一个字符串,再输入一个偏移量,按照偏移量偏移字符串

提示:用Substring分割字符串来做

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("输入字符串");
            string str = Console.ReadLine();//从控制台获取输入字符串
            Console.WriteLine("输入偏移量");
            try
            {
                int b = int.Parse(Console.ReadLine());
                //计算真正的偏移量_bit
                int _bit = b % str.Length;

                string s1 = str.Substring(str.Length-_bit);//从字符串当前位取值到最后
                string s2 = str.Substring(0,str.Length-_bit);//从第0位取值,共str.Length-_bit个
                Console.WriteLine(s1+s2);//输出偏移后的
            }
            catch (Exception)
            {

                Console.WriteLine("输入有误");
            }
            Console.ReadLine();
            
        }
    }

 

posted @ 2016-08-09 20:58  云晴  阅读(1653)  评论(0编辑  收藏  举报