码迷,mamicode.com
首页 > Windows程序 > 详细

C#第六章学习代码

时间:2016-03-30 22:15:31      阅读:372      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6._1_字符及转义字符
{
    class Program
    {
        static void Main(string[] args)
        {

            //char mychar1 = ‘0‘;
            //char mychar2 = ‘a‘;
            //char mychar3 = ‘男‘;
            //int myint = 120;
            //Console.WriteLine(mychar1+"  "+mychar2 + "  " + mychar3);

           
            //char mychar1 = ‘a‘;
            //char mychar2 = ‘0‘;
            //char mychar3 = ‘A‘;
            //if (char.IsLetter(mychar1))

            //Console.WriteLine(char.ToUpper(mychar1));
            //if (char.IsDigit(mychar2)) Console.WriteLine("mychar2是数字");

            //Console.WriteLine(char.ToLower(mychar3));
            Console.WriteLine("这是第一段话\n这是第二段话");
            Console.WriteLine("D:\\Users\\Walker\\Documents\\Visual Studio 2015\\Projects\\51zxwProjects");
            Console.WriteLine(@"D:\Users\Walker\Documents\Visual Studio 2015\Projects\51zxwProjects");
          
            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6._2_字符串简介
{
    class Program
    {
        static void Main(string[] args)
        {
            //string字符串,可以看成是一个没有上限的存储
            //char类型,它是一个0-2^16
            string mystr1 = "这是一句中文";
            string mystr2 = "1234567980";
            string mystr3 = "abcdefg";
            Console.WriteLine(mystr1);
            Console.WriteLine(mystr2);
            Console.WriteLine(mystr3);
            Console.ReadKey();
        }
    }
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6._3_字符串的大小写转换
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("请输入您的英文姓名");
            //string mystr = Console.ReadLine();
            ////ToUpper方法可实现将英文转换为大写
            //if (mystr.ToUpper() == "JACK") Console.WriteLine("这么巧,我也叫JACK");
            //else Console.WriteLine("您的名字很有个性");

            //string mystr = "this is an example of conversation on string";
            //Console.WriteLine("大写字符串:"+mystr.ToUpper());
            //Console.WriteLine("小写字符串:"+mystr.ToLower());

            //char mychar = ‘a‘;
            //Console.WriteLine("小写字符:"+char.ToLower(mychar));
            //Console.WriteLine("大写字符:"+char.ToUpper(mychar));
            //Console.ReadKey();

                Console.ReadKey();







        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6._4_字符串的比较
{
    class Program
    {
        static void Main(string[] args)
        {

            //==  !=

            //Equals
            string mystr1 = "Hello";
            string mystr2 = "HELLO";
            Console.WriteLine(string.Equals(mystr1,mystr2));

            Console.WriteLine(string.Compare(mystr1,mystr2,true));
            Console.WriteLine(mystr1.CompareTo(mystr2));
            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6._5_字符串的格式化
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("{0:C}",21.3);
            //Console.WriteLine("字符串的格式化输出");
            //string s = string.Format("默认格式:-----{0}\n"+"使用C格式:{0:C}\n"+"使用D格式:{0:D}\n"+"使用E格式:{1:E}\n"+"使用F格式:{1:F}\n"+"使用G格式:{0:G}\n"+ "使用N格式:{0:N}\n" + "使用P格式:{0:P1}\n" + "使用X格式:{0:X}\n",2100,2100.33);
            //Console.WriteLine(s);
            for (int i = 001; i < 051; i++)
            {

                string mystr = string.Format("{0:D3}",i);
               Console.WriteLine(mystr);
            }
            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6._6_字符串的截取
{
    class Program
    {
        static void Main(string[] args)
        {
            //string mystr = "walker";
            //string mystr0 = mystr.Substring(2,2);
            //Console.WriteLine(mystr0);
            Console.WriteLine("请输入一个单词,不区分大小写");
            string mystr = Console.ReadLine();
            string firstCapital = mystr.Substring(0, 1).ToUpper();
            string otherCpaital = mystr.Substring(1).ToLower();
            Console.WriteLine("单词首字母大写"+firstCapital+otherCpaital);
            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6._7_字符串的分割
{
    class Program
    {
        static void Main(string[] args)
        {
            string mystr = "51zxw-C#-Walker=6.7字符串的分割";
            Console.WriteLine("原字符串为"+mystr);
            //Console.WriteLine("用-分割后的字符串为:");
            //string[] changeMystr = mystr.Split(‘-‘);
            //foreach (string outstr in changeMystr) Console.Write(outstr+"\t");


            Console.WriteLine("用-和=分割后的字符串为:");
            string[] changeMystr = mystr.Split(new char[] { -,=});
            foreach (string outstr in changeMystr) Console.Write(outstr+"\t");
            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6._._8_字符串的合并
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("字符串的合并");
            string mystr1 = "51zxw";
            string mystr2 = "C#";
            string mystr3 = "Walker";
            string changeMystr = mystr1 + mystr2 + mystr3;
            Console.WriteLine("第一种方法(+)");
            Console.WriteLine(changeMystr);
            Console.WriteLine("第二种方法(Concat)");
            string changeMystr2 = string.Concat(mystr1,mystr2,mystr3);
            Console.WriteLine(changeMystr2);
            Console.WriteLine("第三种方法");
            string[] mystrArray = { "51zxw","C#","walker"};
            string changeMystr3 = string.Join("***", mystrArray);
            Console.WriteLine(changeMystr3);
            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6._9_字符串的插入与填充
{
    class Program
    {
        static void Main(string[] args)
        {

            //字符串的插入
            string mystr = "51zx.net";
            string insertMystr = mystr.Insert(8,"w");
            Console.WriteLine("字符串的插入");
            Console.WriteLine(insertMystr);
            //PadRight用来在字符串的结尾处添加指定(重复)字符以达到指定的总长度,总长度直接用数字来指定
            string padStr = "abc";
            string padStr2 = "abcd";
            string padStr3 = "efghi";
            string padRightStr = padStr.PadRight(6, /)+"\n"+padStr2.PadRight(6,/)+"\n"+padStr3.PadRight(6,/);
            Console.WriteLine("字符串结尾处填充");
            Console.WriteLine(padRightStr);
            //PadLeft用来在字符串的开头处添加指定的字符以达到我们指定的总长度
            string padLeftStr = padStr.PadLeft(6) + "\n" + padStr2.PadLeft(6) + "\n" + padStr3.PadLeft(6);
            Console.WriteLine("字符串开始位置填充");
            Console.WriteLine(padLeftStr);
            

            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6._10_字符串的删除_移除_
{
    class Program
    {
        static void Main(string[] args)
        {
            //通过Remove方法来实现字符串的移除

            //第一种方法 从指定位置开始移除后边的字符串
            string mystr = "xw.net//12345\\5464";
            string changeMystr = mystr.Remove(13);
            Console.WriteLine("用一个参数实现Remove的结果");
            Console.WriteLine(changeMystr);
            //第二种方法 从指定位置开始移除指定位数的字符串
            string mystr2 = "w.comt";
            string changeMystr2 = mystr2.Remove(9,4);
            Console.WriteLine("用两个参数实现Remove的结果");
            Console.WriteLine(changeMystr2);
            Console.ReadKey();

        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6._11_字符串的修剪
{
    class Program
    {
        static void Main(string[] args)
        {

            //字符串的修剪通过Trim()来实现
            string userName = "    walker     ";
            Console.WriteLine("原字符串是:"+userName+"|");
            Console.WriteLine("把空格修剪后是:"+userName.Trim()+"|");
            //修剪开始用TrimStart()
            Console.WriteLine("修剪开始空格后是:"+userName.TrimStart()+"|");

            //修剪结尾用TrimEnd()
            Console.WriteLine("修剪结尾空格后是:" + userName.TrimEnd() + "|");
            //将"#%%##%#http:/t#%"修剪为.net"
            string mywebsite = "#%%##%##%";
            Console.WriteLine("修剪其他字符的方法1:"+mywebsite.Trim(#,%));
            Console.WriteLine("修剪其他字符的方法2:" + mywebsite.Trim("#%".ToCharArray()));

            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6._12_字符串的复制
{
    class Program
    {
        static void Main(string[] args)
        {
            //C#当中复制字符串是通过Copy和CopyTo来实现的
            //Copy  静态方法:string.方法来进行引用
            string mystr = "zxw.net";
            string copyMystr = string.Copy(mystr);
            Console.WriteLine(copyMystr);
            //CopyTo 非静态方法
            char[] mycharArray = new char[20];

            mystr.CopyTo(7,mycharArray,0,13);
            Console.WriteLine("CopyTo实现复制的结果是:");
            Console.WriteLine(mycharArray);
            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6._13_字符串的替换
{
    class Program
    {
        static void Main(string[] args)
        {
            //字符串的替换
            //C#中实现的方法是 非静态方法Replace
       
            string mystr = "xw.net";
            Console.WriteLine("替换前字符串为:"+mystr);
            Console.WriteLine("C#利用Replace进行替换的结果"+mystr.Replace(w, W));
            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6._14_字符串的查找
{
    class Program
    {
        static void Main(string[] args)
        {
            //IndexOf方法
            //string mystr = "12351zxw45651zxw";
            ////              0123456789ABCDEF
            //int myReturnVal1 = mystr.IndexOf("51zxw");
            //int myReturnVal2 = mystr.LastIndexOf("51zxw");

            //Console.WriteLine(myReturnVal1);
            //Console.WriteLine(myReturnVal2);

            Console.WriteLine("请您输入一个字符串,我们来判断是否包含51zxw及它出现第一次和最后一次的位置");
            string mystr = Console.ReadLine();
            int myReturnVal1 = mystr.IndexOf("51zxw");
            int myReturnVal2 = mystr.LastIndexOf("51zxw");
            if (myReturnVal1 < 0) Console.WriteLine("您输入的字符串不包含51zxw");
            else
            {
                Console.WriteLine("您输入的字符串包含51zxw,它第一次出现的位置是第{0}位,最后一次出现的位置是第{1}位", myReturnVal1+1,myReturnVal2+1);

            }
            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6._15_类型转换
{
    class Program
    {
        static void Main(string[] args)
        {
            //类型转换
            //隐式转换
            // char mychar = ‘a‘;
            //int myint = 10;



            //Console.WriteLine(mychar);
            //Console.WriteLine(myint);

            ////myint = mychar;
            ////Console.WriteLine(myint);

            //mychar = checked((char)myint);
            //Console.WriteLine(mychar);
            int myint = 100;
            string mystr = "456";

            //myint = Convert.ToInt32(mystr);
            //mystr = Convert.ToString(myint);
            //mystr = myint.ToString();
            myint = int.Parse(mystr);
            Console.WriteLine(myint);









            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6._16_类型转换_2_
{
    class Program
    {
        static void Main(string[] args)
        {
            double mydouble = 5.5;
            int myint = 100;
            string mystring = "256";
            string stringResult;
            int intResult;
            //string→int

            //(int) 此方法不适用于将string转换为int,只能转换数值类型为int,而不能转换引用类型
            //不会进四舍五入,直接舍掉小数
            //intResult = (int)mydouble;
            //Console.WriteLine(intResult);

            //Convert.ToInt32() 它会将空值NULL转换为0
            //0-4:直接舍掉
            //5:看整数位,4.5 5  6 返回一个临近的偶数
            //6-9:进位

            //intResult = Convert.ToInt32(mydouble);
            //Console.WriteLine(intResult);

            ////int.Parse();不能转换空值 只能将string转换为int
            //intResult = int.Parse(mystring);
            //Console.WriteLine(intResult);

            //int→string
            //强制括号转换只能转换数值类型的 stringResult = (string)myint;
            stringResult = Convert.ToString(myint);

            stringResult = myint.ToString();

           

            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;//引入命名空间
using System.Threading.Tasks;

namespace _6._17_StringBuilder类
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder mystringBuilder = new StringBuilder("1zxw.net");
            Console.WriteLine("=================Append===================");
            //追加对应的字符串
            Console.WriteLine(mystringBuilder.Append("是一个神奇的网站"));
            Console.WriteLine("==================Insert===================");
            //在对应位置插入指定的字符串
            Console.WriteLine(mystringBuilder.Insert(20,"\n"));
            Console.WriteLine("======================Remove==============");
            //移除从索引开始位置的N个字符
            Console.WriteLine(mystringBuilder.Remove(20,1));
            Console.WriteLine("====================Replace===============");
            Console.WriteLine(mystringBuilder.Replace("神奇","强大"));
            Console.WriteLine("======================AppendFormat=======");
            //按照指定的格式来添加对应的字符串
            int saveMoney = 2000;
            Console.WriteLine(mystringBuilder.AppendFormat("并且为我省了{0:C}",saveMoney));
            Console.WriteLine("=========================AppendLine=======");
            Console.WriteLine(mystringBuilder.AppendLine("这句话会自动换行"));

            Console.ReadKey();

        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6._18_本章小结及任务实施
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入两个字符串,每个字符串以回车确认输入,我们将会以\"|\"来进行分隔 :");
            string mystr1 = Console.ReadLine();
            string mystr2 = Console.ReadLine();
            string maxstr;
            if (string.Compare(mystr1, mystr2) > 0) maxstr = mystr1;
            else maxstr = mystr2;
            Console.WriteLine("您输入的两个字符串分别是:{0},{1}",mystr1,mystr2);
            //利用SubString来进行分隔
            Console.WriteLine("拆分较大字符串后的结果是:");
            for (int i = 0; i < maxstr.Length; i++)
            {
                Console.Write(maxstr.Substring(i, 1));
                if(i!=maxstr.Length-1)Console.Write("|");
            }
            Console.ReadKey();
        }
    }
}

 

C#第六章学习代码

标签:

原文地址:http://www.cnblogs.com/zqyo2000z/p/5338869.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!