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

C# 字符串详细使用

时间:2014-10-12 01:47:37      阅读:400      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   使用   ar   java   

转自 http://www.cnblogs.com/candywyq/archive/2007/07/24/830021.html

1.Convert.ToInt32与Int32.Parse的恩恩怨怨 2.Split的3种用法 3.@"abc"和"abc"区别在那里 4.保留2位有效小数及四舍五入 5.url传递中文的解决方案 6.把123456789转换为12-345-6789的3种方法 7.交换两个指定位置字符的4种方法 8.“%10”的妙用 9.输出21个AAAAAAAAAAAAAAAAAAAAA的巧妙做法

 

1.Convert.ToInt32Int32.Parse的恩恩怨怨

这2个方法都可以把把string解析为int,那么我们一定会有疑问:到底他们有什么区别?什么时候该用什么?性能如何等等。

其实在2.0里还有Int32.TryParse也实现了同样的效果。

 

bubuko.com,布布扣using System; bubuko.com,布布扣using System.Collections.Generic; bubuko.com,布布扣using System.Text; bubuko.com,布布扣 bubuko.com,布布扣namespace ConsoleApplication1 bubuko.com,布布扣{ bubuko.com,布布扣    class Program bubuko.com,布布扣    { bubuko.com,布布扣        static void Main(string[] args) bubuko.com,布布扣        { bubuko.com,布布扣            string myString = "1234"; bubuko.com,布布扣            int myint = 0; bubuko.com,布布扣 bubuko.com,布布扣            myint = Convert.ToInt32(myString); bubuko.com,布布扣            Console.Write(myint+"\r\n "); bubuko.com,布布扣 bubuko.com,布布扣            myint = Int32.Parse(myString); bubuko.com,布布扣            Console.Write(myint+"\r\n "); bubuko.com,布布扣 bubuko.com,布布扣            Int32.TryParse(myString, out myint); bubuko.com,布布扣            Console.Write(myint+"\r\n"); bubuko.com,布布扣 bubuko.com,布布扣        } bubuko.com,布布扣    } bubuko.com,布布扣}

bubuko.com,布布扣

表面上看,可见3个方法都实现了同样的效果!

那么我们把代码改一下:

            //string myString = "1234";             string myString = null;             int myint = 0;

            myint = Convert.ToInt32(myString);             Console.Write(myint+"\r\n");

            myint = Int32.Parse(myString);             Console.Write(myint+"\r\n");

            Int32.TryParse(myString, out myint);             Console.Write(myint+"\r\n"); 这次字符串是null,那么运行结果会怎样呢? bubuko.com,布布扣

这是因为如果解析错误: Convert.ToInt32()null时不抛异常而是返回0; Int32.Parse()抛异常Int32.TryParse()不抛异常,会返回true或false来说明解析是否成功,如果解析错误,调用方将会得到0值。

由于Convert.ToInt32()在null时我们看不到Int32.TryParse()的运行结果所以再分调试和不调试来看结果的差异: 调试: bubuko.com,布布扣

不调试: bubuko.com,布布扣

其实一般出bug毕竟属于少数,而且大家都会测试保证不出bug,那么我们最关心的或许就是性能。

再把代码修改一下: 2

bubuko.com,布布扣            string myString1 = "1234"; bubuko.com,布布扣            //string myString = null;//清清月儿 http://blog.csdn.net/21aspnet/ bubuko.com,布布扣            int myint = 0; bubuko.com,布布扣            Console.Write(System.DateTime.Now.ToString()+"  "+System.DateTime.Now.Millisecond.ToString() + " "); bubuko.com,布布扣            for (int i = 0; i < 1000000;i++ ) bubuko.com,布布扣            { bubuko.com,布布扣                myint = Convert.ToInt32(myString1); bubuko.com,布布扣            } bubuko.com,布布扣            Console.Write(myint + " \r\n"); bubuko.com,布布扣            Console.Write(System.DateTime.Now.ToString() + "  " + System.DateTime.Now.Millisecond.ToString() + "\r\n "); bubuko.com,布布扣 bubuko.com,布布扣 bubuko.com,布布扣            string myString2 = "1234"; bubuko.com,布布扣            for (int i = 0; i < 1000000; i++) bubuko.com,布布扣            { bubuko.com,布布扣                myint = Int32.Parse(myString2); bubuko.com,布布扣            } bubuko.com,布布扣            Console.Write(myint + "\r\n "); bubuko.com,布布扣            Console.Write(System.DateTime.Now.ToString() + "  " + System.DateTime.Now.Millisecond.ToString() + " "); bubuko.com,布布扣 bubuko.com,布布扣            string myString3 = "1234"; bubuko.com,布布扣            for (int i = 0; i < 1000000; i++) bubuko.com,布布扣            { bubuko.com,布布扣                Int32.TryParse(myString3, out myint); bubuko.com,布布扣            } bubuko.com,布布扣            Console.Write(myint + "\r\n "); bubuko.com,布布扣            Console.Write(System.DateTime.Now.ToString() + "  " + System.DateTime.Now.Millisecond.ToString() + "\r\n "); bubuko.com,布布扣

 

我们让3个方法执行100万次转换毫秒差异: bubuko.com,布布扣

为了准确多做几次测试: 第二次 bubuko.com,布布扣

第三次 bubuko.com,布布扣

 

  第一次 第二次 第三次
Convert.ToInt32() 532-204=328 1163-750=413 782-469=313
Int32.Parse() 844-532=312 360-63=297 1094-782=312
Int32.TryParse() 1141-844=297 657-360=297 375-94=281

 

其实我们可以得出结论: 3个方法几乎没有差异! 如果真要追求完美那么性能的差异是:Int32.TryParse()优于Int32.Parse()优于Convert.ToInt32()。 所以个人建议:.NET1.1下用Int32.Parse();.NET2.0用Int32.TryParse()。

那么为什么会这样呢? 其实这些数据不是偶然的,因为: Convert.ToInt32 会把最终的解析工作代理给 Int32.Parse; Int32.Parse 会把最终的解析工作代理给Number.ParseInt32; Int32.TryParse 会把最终的解析工作代理给Number.TryParseInt32。

至于其他进制的转换请参考MSDN。重载参数即可!

2.Split的3种用法

我们可以把12     33   456    12342  拼起来就是一个字符,因为很多时候我们觉得处理只有几个组的时候用数组很麻烦所以我们用“|”或者“,”等等把他们拼起来在需要的时候用Split打散即可。//清清月儿 http://blog.csdn.net/21aspnet/

下面列举一些用法,不做性能分析了。 方法一:

bubuko.com,布布扣static void Main(string[] args) bubuko.com,布布扣        { bubuko.com,布布扣            string aa = "1234,234523,4324,324";//清清月儿 http://blog.csdn.net/21aspnet/ bubuko.com,布布扣            string[] cc = aa.Split(new char[] { ‘,‘ }); bubuko.com,布布扣            foreach (string bb in cc) bubuko.com,布布扣            { bubuko.com,布布扣                Console.Write(bb + "\r\n "); bubuko.com,布布扣            } bubuko.com,布布扣           bubuko.com,布布扣        }

方法二:

bubuko.com,布布扣string aa = "1234,234523,4324,324"; bubuko.com,布布扣            string[] str = aa.Split(‘,‘); bubuko.com,布布扣            foreach (string bb in str) bubuko.com,布布扣            { bubuko.com,布布扣                Console.Write(bb + " \r\n"); bubuko.com,布布扣            }

 

方法三:

bubuko.com,布布扣static void Main(string[] args) bubuko.com,布布扣        { bubuko.com,布布扣            string a = "1,2,3,4,5,6,7,8,9"; bubuko.com,布布扣            string b = ","; bubuko.com,布布扣            string[] c = Split(a, b); bubuko.com,布布扣            foreach (string bb in c) bubuko.com,布布扣            { bubuko.com,布布扣                Console.Write(bb + " \r\n"); bubuko.com,布布扣            } bubuko.com,布布扣 bubuko.com,布布扣        } bubuko.com,布布扣        public static string[] Split(string input, string pattern) bubuko.com,布布扣        { bubuko.com,布布扣            string[] arr = System.Text.RegularExpressions.Regex.Split(input, pattern); bubuko.com,布布扣            return arr; bubuko.com,布布扣        }

实现把文章按空格打散:

bubuko.com,布布扣public static void Main () bubuko.com,布布扣    { bubuko.com,布布扣        string a="While laughter is is very aerobic activity engages every single organ system"; bubuko.com,布布扣        string b=" "; bubuko.com,布布扣        string []c=TestDoWhile.Split(a,b); bubuko.com,布布扣        foreach(string bb in c) bubuko.com,布布扣        { bubuko.com,布布扣            Console.Write(bb+" \r\n"); bubuko.com,布布扣        } bubuko.com,布布扣    } bubuko.com,布布扣    public static string[] Split(string input,string pattern) bubuko.com,布布扣    { bubuko.com,布布扣        string[] arr = System.Text.RegularExpressions.Regex.Split(input,pattern); bubuko.com,布布扣        return arr; bubuko.com,布布扣    }

bubuko.com,布布扣

3.@"abc"和"abc"区别在那里 @"abc"和"abc"没有什么区别

不过@是忽略转义字符的! 比如 "abc\n" 输出 abc 并加个换行 但是 @"abc\n" 输出 abc\n 无换行! 取消转义

比如你想将 C:\windows\system 这个字符串赋值给 str

一般情况下要: string str = "C:\\windows\\system";

因为在C#中,\\ 才会被转义成一个 \ 又例如\n就是换行符

而加入@,就可以这样:

string str = @"C:\windows\system";

4.保留2位有效小数及四舍五入 这又是一个经常遇到的问题。

保留2位有效小数(和保留N位一样的参数不同而已): bubuko.com,布布扣

bubuko.com,布布扣 static void Main() bubuko.com,布布扣        { bubuko.com,布布扣            Double a = 12.345678; bubuko.com,布布扣            Console.Write(Math.Round(a,2)); bubuko.com,布布扣        }

 

四舍五入:

bubuko.com,布布扣static void Main() bubuko.com,布布扣        { bubuko.com,布布扣 bubuko.com,布布扣            Double a = 12.345678;//是要四舍五入的数 bubuko.com,布布扣            Console.Write((a*10000+0.5)/10000); bubuko.com,布布扣        }

bubuko.com,布布扣

5.url传递中文的解决方案 1.设置web.config文件。 <system.web> ...... <globalization requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN" fileEncoding="gb2312" /> ...... </system.web> 2.传递中文之前,将要传递的中文参数进行编码,在接收时再进行解码。 >> 进行传递 string Name = "中文参数"; Response.Redirect("B.aspx?Name="+Server.UrlEncode(Name)); >> 进行接收 string Name = Request.QueryString["Name"]; Response.Write(Server.UrlDecode(Name));

3.如果是从 .HTML 文件向 .Aspx 文件进行传递中文参数的话(即不从后台用 Redirect()方法进行 Url 转换)。一样要将传递的中文参数进行编码,在接收时再进行解码。 >> 进行传递 <script language="JavaScript"> function GoUrl() { var Name = "中文参数"; location.href = "B.aspx?Name="+escape(Name); } </script> <body onclick="GoUrl()"> >> 进行接收 string Name = Request.QueryString["Name"]; Response.Write(Server.UrlDecode(Name));

一般来说。设置web.config文件就可以了。但是如果你用 JavaScript 调用 webservice 方法的话(往webservice里面传递中文参数)。设置 web.config 文件好象无效。 //清清月儿http://blog.csdn.net/21aspnet/

6.把123456789转换为12-345-6789的3种方法
方法一:

bubuko.com,布布扣            string a = "123456789"; bubuko.com,布布扣            a = int.Parse(a).ToString("##-###-####"); bubuko.com,布布扣            Console.Write(a); bubuko.com,布布扣

 

方法二:

bubuko.com,布布扣string a = "123456789"; bubuko.com,布布扣            a = a.Insert(5, "-").Insert(2, "-"); bubuko.com,布布扣            Console.Write(a);

方法三:

bubuko.com,布布扣using System; bubuko.com,布布扣using System.Collections.Generic; bubuko.com,布布扣using System.Text; bubuko.com,布布扣using System.Text.RegularExpressions; bubuko.com,布布扣namespace ConsoleApplication1 bubuko.com,布布扣{ bubuko.com,布布扣    class Program bubuko.com,布布扣    { bubuko.com,布布扣        static void Main() bubuko.com,布布扣        { bubuko.com,布布扣            string a = "123456789"; bubuko.com,布布扣 bubuko.com,布布扣            Regex reg = new Regex(@"^(d{2})(d{3})(d{4})$"); bubuko.com,布布扣            a = reg.Replace(a, "$1-$2-$3"); bubuko.com,布布扣            Console.Write(a); bubuko.com,布布扣        } bubuko.com,布布扣        bubuko.com,布布扣    } bubuko.com,布布扣}

bubuko.com,布布扣

7.交换两个指定位置字符的4种方法
方法一:

bubuko.com,布布扣static void Main() bubuko.com,布布扣        { bubuko.com,布布扣            string s = "123456789"; bubuko.com,布布扣            SwapChar(ref s, 3, 6); bubuko.com,布布扣            Console.Write(s.ToString()); bubuko.com,布布扣        } bubuko.com,布布扣 bubuko.com,布布扣        static void SwapChar(ref string s, int i1, int i2) bubuko.com,布布扣        { bubuko.com,布布扣            char temp = s[i1]; bubuko.com,布布扣            char temp1 = s[i2]; bubuko.com,布布扣            s = s.Remove(i1, 1).Insert(i1, temp1.ToString()); bubuko.com,布布扣            s = s.Remove(i2, 1).Insert(i2, temp.ToString()); bubuko.com,布布扣        }

 

方法二:

bubuko.com,布布扣 static void Main() bubuko.com,布布扣        { bubuko.com,布布扣            string s = "123456789"; bubuko.com,布布扣            //SwapChar(s, 3, 6); bubuko.com,布布扣            Console.Write(SwapChar(s, 3, 6).ToString()); bubuko.com,布布扣        } bubuko.com,布布扣 bubuko.com,布布扣        static string SwapChar(string s, int p1, int p2) bubuko.com,布布扣        { bubuko.com,布布扣            if ((p1 == p2) || ((p1 < 0) || (p2 < 0))) return s; bubuko.com,布布扣            if ((p1 >= s.Length) || (p2 >= s.Length)) return s; bubuko.com,布布扣            char[] vChars = s.ToCharArray(); bubuko.com,布布扣            vChars[p1] = (char)(vChars[p2] | (vChars[p2] = vChars[p1]) & 0); bubuko.com,布布扣            return new string(vChars); bubuko.com,布布扣        }

bubuko.com,布布扣

方法三:

bubuko.com,布布扣static void Main() bubuko.com,布布扣        { bubuko.com,布布扣            string s = "123456789"; bubuko.com,布布扣            Console.Write(SwapChar(s, 3, 6).ToString()); bubuko.com,布布扣        } bubuko.com,布布扣 bubuko.com,布布扣        public static string SwapChar(string str, int a, int b) bubuko.com,布布扣        { bubuko.com,布布扣            char[] newStr = str.ToCharArray(); bubuko.com,布布扣            newStr[a] = str[b]; bubuko.com,布布扣            newStr[b] = str[a]; bubuko.com,布布扣            return new string(newStr); bubuko.com,布布扣        } bubuko.com,布布扣

 

方法四:

bubuko.com,布布扣using System; bubuko.com,布布扣using System.Collections.Generic; bubuko.com,布布扣using System.Text; bubuko.com,布布扣using System.Text.RegularExpressions; bubuko.com,布布扣namespace ConsoleApplication1 bubuko.com,布布扣{ bubuko.com,布布扣    class Program bubuko.com,布布扣    { bubuko.com,布布扣        static void Main() bubuko.com,布布扣        { bubuko.com,布布扣            string s = "123456789"; bubuko.com,布布扣            Console.Write(SwapChar(s, 3, 6).ToString()); bubuko.com,布布扣        } bubuko.com,布布扣 bubuko.com,布布扣        static string SwapChar(string s, int p1, int p2) bubuko.com,布布扣        { bubuko.com,布布扣            if (p1 > p2) { int p = p1; p1 = p2; p2 = p; } bubuko.com,布布扣            return Regex.Replace(s, "^(.{" + p1 + "})(.)(.{" + (p2 - p1 - 1) + "})(.)(.*)$", "$1$4$3$2$5"); bubuko.com,布布扣        } bubuko.com,布布扣 bubuko.com,布布扣    } bubuko.com,布布扣}

 

8. “%10”的妙用

bubuko.com,布布扣

bubuko.com,布布扣static void Main() bubuko.com,布布扣        { bubuko.com,布布扣 bubuko.com,布布扣            Random r = new Random(); bubuko.com,布布扣 bubuko.com,布布扣            Console.WriteLine(r.Next() % 10);//1-9 bubuko.com,布布扣            Console.WriteLine(r.Next() % 10); bubuko.com,布布扣            Console.WriteLine(r.Next() % 10); bubuko.com,布布扣            Console.WriteLine(r.Next() % 10); bubuko.com,布布扣            Console.WriteLine(r.Next() % 10); bubuko.com,布布扣            Console.WriteLine(r.Next() % 10); bubuko.com,布布扣            Console.WriteLine(r.Next() % 10); bubuko.com,布布扣            Console.WriteLine(r.Next() % 10); bubuko.com,布布扣            Console.WriteLine(r.Next() % 10); bubuko.com,布布扣        }

 

9.输出21个AAAAAAAAAAAAAAAAAAAAA的巧妙做法

bubuko.com,布布扣

new构造器的理解 如果要你创建一个由21个"A"字符构成的字符串,你会怎么做? string str = "AAAAAAAAAAAAAAAAAAAAA";//老实型 string str = new string(‘A‘, 21);//简单聪明

C# 字符串详细使用

标签:style   blog   http   color   io   os   使用   ar   java   

原文地址:http://www.cnblogs.com/izhaofu/p/4019981.html

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