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

        这年头,写点东西其实也挺不容易的,很多文章大家都只是为了做个笔记,而我这种,现在不太需要什么笔记,但是真正要专门写点什么,却又发现该写的都让人写完了,于是还是从最基本的控制台应用程序开始来讲一讲字符串的基本处理方法。

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

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

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

        我们首先来看一段代码:

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

        输入:Jerry

        输出:Hello Jerry

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

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

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,以便得到授权之后跳转到我们制定的不同页面。

        当然,格式化还有很多不同的格式,以及用途,不再逐个讲解,其他的格式化(比如格式化保留几位小数,格式化时间等等)可以等你用到的时候再进行查询。希望以此抛砖引玉。

原文地址:https://www.cnblogs.com/WangJerry/p/5130175.html