C# 字符串详细使用(转)
2010-01-22 16:07
295 查看
C# 字符串详细使用(转)
开发中可以说几乎随时会涉及到字符串处理,本人觉得很有必要把平时遇到的问题和大家一起讨论,如果大家有好的见解和心得请留言和大家分享。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也实现了同样的效果。
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
string myString1 = "1234";
//string myString = null;//清清月儿 http://blog.csdn.net/21aspnet/ int myint = 0;
Console.Write(System.DateTime.Now.ToString()+" "+System.DateTime.Now.Millisecond.ToString() + " ");
for (int i = 0; i < 1000000;i++ )
Console.Write(myint + " \r\n");
Console.Write(System.DateTime.Now.ToString() + " " + System.DateTime.Now.Millisecond.ToString() + "\r\n ");
string myString2 = "1234";
for (int i = 0; i < 1000000; i++)
Console.Write(myint + "\r\n ");
Console.Write(System.DateTime.Now.ToString() + " " + System.DateTime.Now.Millisecond.ToString() + " ");
string myString3 = "1234";
for (int i = 0; i < 1000000; i++)
Console.Write(myint + "\r\n ");
Console.Write(System.DateTime.Now.ToString() + " " + System.DateTime.Now.Millisecond.ToString() + "\r\n ");
我们让3个方法执行100万次转换看毫秒的差异:
static void Main(string[] args)
string aa = "1234,234523,4324,324";
string[] str = aa.Split(',');
foreach (string bb in str)
static void Main(string[] args)
public static string[] Split(string input, string pattern)
public static void Main ()
public static string[] Split(string input,string pattern)
static void Main()
static void Main()
string a = "123456789";
a = int.Parse(a).ToString("##-###-####");
Console.Write(a);
方法二:
string a = "123456789";
a = a.Insert(5, "-").Insert(2, "-");
Console.Write(a);
方法三:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
static void Main()
static void SwapChar(ref string s, int i1, int i2)
static void Main()
static string SwapChar(string s, int p1, int p2)
static void Main()
public static string SwapChar(string str, int a, int b)
方法四:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
static void Main()
{
Random r = new Random();
Console.WriteLine(r.Next() % 10);//1-9
Console.WriteLine(r.Next() % 10);
Console.WriteLine(r.Next() % 10);
Console.WriteLine(r.Next() % 10);
Console.WriteLine(r.Next() % 10);
Console.WriteLine(r.Next() % 10);
Console.WriteLine(r.Next() % 10);
Console.WriteLine(r.Next() % 10);
Console.WriteLine(r.Next() % 10);
}
[b]9.输出21个AAAAAAAAAAAAAAAAAAAAA的巧妙做法
new构造器的理解
如果要你创建一个由21个"A"字符构成的字符串,你会怎么做?
string str = "AAAAAAAAAAAAAAAAAAAAA";//老实型
string str = new string('A', 21);//简单聪明
相关文章推荐
- C# 字符串详细使用
- C# 字符串详细使用(转)
- C# 字符串详细使用(转)
- C# 字符串处理的详细使用(转)
- C# 字符串详细使用
- C# 字符串详细使用(转)
- C#字符串的使用笔记
- C# 关于如何将字符串的地址赋给char*使用
- Unity 3D: c#中字符串截取使用的方法
- 使用C#,不使用任何Parse,TryParse,Convert方法,将数字字符串转换成整数
- [置顶] [C#] delegate/event 的详细使用方法介绍与例程
- 使用C#格式化字符串 ~
- C#使用SendMessage传递字符串
- C#使用ODBCl连接MySql详细教程
- c#中字符串截取使用的方法
- 使用WCF传输DataTable:DataTable和Xml格式的字符串相互转换(C#)
- 使用C#将字符串转换成十六进制以及其逆转换
- C#字符串使用的一些小建议
- C# 使用正则表达式去掉字符串中的数字
- 使用C#格式化字符串 ~