C#入门——字符串处理方法(格式化输出)

        这年头,写点东西其实也挺不容易的,不少文章你们都只是为了作个笔记,而我这种,如今不太须要什么笔记,可是真正要专门写点什么,却又发现该写的都让人写完了,因而仍是从最基本的控制台应用程序开始来说一讲字符串的基本处理方法。api

        C#开发的应用程序绝大多数都在处理文字信息,其实说白了就是字符串的处理,不知道是否是由于讲师大多从C++或者Java语言过来的缘故,不少开发者一用到字符串处理,就开始用加号:微信

string str = "Hello";
string result = str + " " + name;

        虽然这种写法也不算错,可是在C#语言(不要讨论是ASP.NET平台提供仍是C#语言提供)中,确实提供不少字符串处理方法,让咱们更能以有效地简洁地方式处理字符串。app

        咱们首先来看一段代码:微信公众平台

string name = Console.ReadLine();
Console.WriteLine("Hello {0}", name);

        输入:Jerryspa

        输出:Hello Jerrycode

        这种输出咱们称之为格式化输出,即:咱们先定义整个字符串的格式,而后把参数像填空同样,填写完善,最后输出。orm

        为了显示更多比较灵活的应用,咱们改用string.Format方法来进行字符串的格式化:blog

string name = Console.ReadLine();
string result = string.Format("Hello {0}", name);
Console.WriteLine(result);

        输出的结果,没有任何的变化,也就是说Format方法也能帮咱们把事先定义好的字符串进行格式化。接口

        接下来咱们看几段代码,来分析格式化输出定义的具体含义:开发

        1.

string name = Console.ReadLine();
string welcome = "welcome to C#";
string result = string.Format("Hello {0}, {1}", name, welcome);
Console.WriteLine(result);

        输入:Jerry

        输出:Hello Jerry,welcome to C#

        2.

string name = Console.ReadLine();
string welcome = "welcome to C#";
string result = string.Format("Hello {1}", name, welcome);
Console.WriteLine(result);

        输入:Jerry

        输出:Hello welcome to C#

        3.

string name = Console.ReadLine();
string welcome = "welcome to C#";
string result = string.Format("Hello {0}", name, welcome);
Console.WriteLine(result);

        输入:Jerry

        输出:Hello Jerry

        4.

string name = Console.ReadLine();
string welcome = "welcome to C#";
string result = string.Format("Hello {1}", name);
Console.WriteLine(result);

        输入:Jerry

        输出:没有输出,报异常了

        若是你一个一个验证过这些代码,第四段代码的确报错了,那么总结下来,{n}应该算是定义格式化字符串的占位符,n表示后续参数的位置,n从0开始,若是0超出后续参数个数,则会报错。

        既然{n}表示格式化,若是我有输出花括弧符号"{}"的需求该怎么办呢?再来看两段代码:

        1.

string name = Console.ReadLine();
string welcome = "welcome to C#";
string result = string.Format("Hello {{0}}, {1}", name, welcome);
Console.WriteLine(result);

        输入:Jerry

        输出:Hello {0},welcome to C#


        2.

string name = Console.ReadLine();
string welcome = "welcome to C#";
string result = string.Format("Hello {{{0}}}, {1}", name, welcome);
Console.WriteLine(result);

        输入:Jerry

        输出:Hello {Jerry},welcome to C#

        由此咱们发现,在格式化方法中,{{}}会被解释为一对{}。

        那么,这些内容究竟有哪些用途呢?若是没有记错,微信公众平台的用户受权接口地址给的是:

         https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

        咱们能够将此字符串简化为:

        https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={{0}}&response_type={1}&scope={2}&state={3}#wechat_redirect

        首次处理,咱们能够格式化appid、response_type、scope、state获得一下结果

        https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx_your_id&redirect_uri={0}&response_type=code&scope=snsapi_userinfo&state=param_state#wechat_redirect

        接下来,咱们在不一样的页面须要获取受权的话,即可将该结果,进行再次format,以便获得受权以后跳转到咱们制定的不一样页面。

        固然,格式化还有不少不一样的格式,以及用途,再也不逐个讲解,其余的格式化(好比格式化保留几位小数,格式化时间等等)能够等你用到的时候再进行查询。但愿以此抛砖引玉。

相关文章
相关标签/搜索