benben 发表于 2013-2-4 23:52:10

C#优化字符串操作【月儿原创】

C#优化字符串操作

作者:清清月儿
主页:http://blog.csdn.net/21aspnet/           时间:2007.6.17
        开发中可以说几乎随时会涉及到字符串处理,本人觉得很有必要把平时遇到的问题和大家一起讨论,如果大家有好的见解和心得请留言和大家分享。
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.ToInt32与Int32.Parse的恩恩怨怨
这2个方法都可以把把string解析为int,那么我们一定会有疑问:到底他们有什么区别?什么时候该用什么?性能如何等等。
其实在2.0里还有Int32.TryParse也实现了同样的效果。
<span style="color: #000000;" />
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System.Collections.Generic;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System.Text;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifnamespace ConsoleApplication1
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    class Program
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        static void Main(string[] args)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            string myString = "1234";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            int myint = 0;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            myint = Convert.ToInt32(myString);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            Console.Write(myint+"\r\n ");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            myint = Int32.Parse(myString);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            Console.Write(myint+"\r\n ");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            Int32.TryParse(myString, out myint);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            Console.Write(myint+"\r\n");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
页: [1]
查看完整版本: C#优化字符串操作【月儿原创】