C#字符串類String的使用(二)

格式化字符串
//格式化字符串
            //一個靜態的Format方法,用於將字符串數據格式化成指定的格式
            string newstr = String.Format("{0},{1}!!!", str1, str2);
            Console.WriteLine(newstr);
            Console.ReadLine();
//Format方法將日期格式格式化成指定格式
            DateTime dt = DateTime.Now;
            string strb = String.Format("{0:D}", dt);
            Console.WriteLine(strb);
            Console.ReadLine();
截取字符串
//截取字符串
            //Substring 該方法能夠截取字符串中指定位置的指定長度的字符串
            //索引從0開始截取三個字符
            string a = "zhzfdx@qq.com";
            string b = "";
            b = a.Substring(0, 3);
            Console.WriteLine(b);
            Console.ReadLine();
分割字符串
//分割字符串
            //Split 用於分割字符串,此方法的返回值包含全部分割子字符串的數組對象
            //能夠經過數組取得全部分割的子字符串
            string a = "zhzfdx@qq.com";
            string[] splitstrings = new string[100];
            char[] separator = { '@', '.' };
            splitstrings = a.Split(separator);
            for(int i = 0; i < splitstrings.Length; i++)
            {
                Console.WriteLine("item{0}:{1}", i, splitstrings[i]);
            }
            Console.ReadLine();
插入和填充字符串
//插入和填充字符串
            //Insert方法 用於向字符串任意位置插入新元素
            //插入
            string a = "zhz";
            string b;
            b = a.Insert(3, "fdx@qq.com");
            Console.WriteLine(b);
            Console.ReadLine();
//PadLeft/PadRight方法用於填充字符串
            //PadLeft方法是在字符串左側進行字符填充
            //PadRight方法是在字符串右側進行字符填充
            //注意:字符填充!!!
            string a = "";
            string b = a.PadLeft(1, 'z');
            b = b.PadRight(2, 'h');
            Console.WriteLine(b);
            Console.ReadLine();
刪除字符串
//刪除字符串
            //Remove 方法從一個字符串指定位置開始,刪除指定數量的字符

            string a = "zhzfdx@qq.com";
            string b = a.Remove(3);
            string c = a.Remove(3, 3);
            Console.WriteLine(b);
            Console.WriteLine(c);
            Console.ReadLine();
複製字符串
//複製字符串
            //Copy和CopyTo方法
            //Copy用於將字符串或子字符串複製到另外一個字符串或Char類型的數組中
            //CopyTo能夠將字符串的某一部分複製到另外一個數組中
            //Copy
            string a = "zhzfdx@qq.com";
            string b;
            b = String.Copy(a);
            Console.WriteLine(a);
            Console.WriteLine(b);
            Console.ReadLine();

            //CopyTo
            char[] str = new char[100];
            //3:須要複製的字符的起始位置3
            //str:目標數組
            //0:目標數組中開始存放的位置0
            //10:複製字符的個數
            a.CopyTo(3, str, 0, 10);
            Console.WriteLine(str);
            Console.ReadLine();
替換字符串
//替換字符串
            //Replace方法
            //將字符串中的某個字符或字符串替換成其餘字符或字符串

            string a = "zzzhhhzzz";
            string b = a.Replace('z', 'x');
            Console.WriteLine(b);
            string c = a.Replace("zzzhhhzzz", "fffdddxxx");
            Console.WriteLine(c);
            Console.ReadLine();

未完待續。。。。。。數組

相關文章
相關標籤/搜索