转自:http://www.builder.com.cn/2008/0218/738288.shtml
------------------------------------------------------------------------------------------------------------------------------------
我们可以把12 33 456 12342 拼起来就是一个字符,因为很多时候我们觉得处理只有几个组的时候用数组很麻烦所以我们用“|”或者“,”等等把他们拼起来在需要的时候用Split打散即可。//清清月儿 http://blog.csdn.net/21aspnet/
方法一:
1 static void Main(string[] args)
2 {
3 string aa = "1234,234523,4324,324";//清清月儿 http://blog.csdn.net/21aspnet/
4 string[] cc = aa.Split(new char[] { ',' });
5 foreach (string bb in cc)
6 {
7 Console.Write(bb + "rn ");
8 }
9
10 }
方法二:2 {
3 string aa = "1234,234523,4324,324";//清清月儿 http://blog.csdn.net/21aspnet/
4 string[] cc = aa.Split(new char[] { ',' });
5 foreach (string bb in cc)
6 {
7 Console.Write(bb + "rn ");
8 }
9
10 }
1 string aa = "1234,234523,4324,324";
2 string[] str = aa.Split(',');
3 foreach (string bb in str)
4 {
5 Console.Write(bb + " rn");
6 }
方法三:
2 string[] str = aa.Split(',');
3 foreach (string bb in str)
4 {
5 Console.Write(bb + " rn");
6 }
1 static void Main(string[] args)
2 {
3 string a = "1,2,3,4,5,6,7,8,9";
4 string b = ",";
5 string[] c = Split(a, b);
6 foreach (string bb in c)
7 {
8 Console.Write(bb + " rn");
9 }
10
11 }
12 public static string[] Split(string input, string pattern)
13 {
14 string[] arr = System.Text.RegularExpressions.Regex.Split(input, pattern);
15 return arr;
16 }
实现把文章按空格打散:
2 {
3 string a = "1,2,3,4,5,6,7,8,9";
4 string b = ",";
5 string[] c = Split(a, b);
6 foreach (string bb in c)
7 {
8 Console.Write(bb + " rn");
9 }
10
11 }
12 public static string[] Split(string input, string pattern)
13 {
14 string[] arr = System.Text.RegularExpressions.Regex.Split(input, pattern);
15 return arr;
16 }
1 public static void Main ()
2 {
3 string a="While laughter is is very aerobic activity engages every single organ system";
4 string b=" ";
5 string []c=TestDoWhile.Split(a,b);
6 foreach(string bb in c)
7 {
8 Console.Write(bb+" rn");
9 }
10 }
11 public static string[] Split(string input,string pattern)
12 {
13 string[] arr = System.Text.RegularExpressions.Regex.Split(input,pattern);
14 return arr;
15 }
6.把123456789转换为12-345-6789的3种方法2 {
3 string a="While laughter is is very aerobic activity engages every single organ system";
4 string b=" ";
5 string []c=TestDoWhile.Split(a,b);
6 foreach(string bb in c)
7 {
8 Console.Write(bb+" rn");
9 }
10 }
11 public static string[] Split(string input,string pattern)
12 {
13 string[] arr = System.Text.RegularExpressions.Regex.Split(input,pattern);
14 return arr;
15 }
方法一:
1 string a = "123456789";
2 a = int.Parse(a).ToString("##-###-####");
3 Console.Write(a);
方法二:2 a = int.Parse(a).ToString("##-###-####");
3 Console.Write(a);
1 string a = "123456789";
2 a = a.Insert(5, "-").Insert(2, "-");
3 Console.Write(a);
方法三:
2 a = a.Insert(5, "-").Insert(2, "-");
3 Console.Write(a);
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Text.RegularExpressions;
5 namespace ConsoleApplication1
6 {
7 class Program
8 {
9 static void Main()
10 {
11 string a = "123456789";
12
13 Regex reg = new Regex(@"^(d)(d)(d)$");
14 a = reg.Replace(a, "--");
15 Console.Write(a);
16 }
17
18 }
19 }
7.交换两个指定位置字符的4种方法2 using System.Collections.Generic;
3 using System.Text;
4 using System.Text.RegularExpressions;
5 namespace ConsoleApplication1
6 {
7 class Program
8 {
9 static void Main()
10 {
11 string a = "123456789";
12
13 Regex reg = new Regex(@"^(d)(d)(d)$");
14 a = reg.Replace(a, "--");
15 Console.Write(a);
16 }
17
18 }
19 }
方法一:
1 static void Main()
2 {
3 string s = "123456789";
4 SwapChar(ref s, 3, 6);
5 Console.Write(s.ToString());
6 }
7
8 static void SwapChar(ref string s, int i1, int i2)
9 {
10 char temp = s[i1];
11 char temp1 = s[i2];
12 s = s.Remove(i1, 1).Insert(i1, temp1.ToString());
13 s = s.Remove(i2, 1).Insert(i2, temp.ToString());
14 }
方法二:2 {
3 string s = "123456789";
4 SwapChar(ref s, 3, 6);
5 Console.Write(s.ToString());
6 }
7
8 static void SwapChar(ref string s, int i1, int i2)
9 {
10 char temp = s[i1];
11 char temp1 = s[i2];
12 s = s.Remove(i1, 1).Insert(i1, temp1.ToString());
13 s = s.Remove(i2, 1).Insert(i2, temp.ToString());
14 }
1 static void Main()
2 {
3 string s = "123456789";
4 //SwapChar(s, 3, 6);
5 Console.Write(SwapChar(s, 3, 6).ToString());
6 }
7
8 static string SwapChar(string s, int p1, int p2)
9 {
10 if ((p1 == p2) || ((p1 < 0) || (p2 < 0))) return s;
11 if ((p1 >= s.Length) || (p2 >= s.Length)) return s;
12 char[] vChars = s.ToCharArray();
13 vChars[p1] = (char)(vChars[p2] | (vChars[p2] = vChars[p1]) & 0);
14 return new string(vChars);
15 }
方法三:
2 {
3 string s = "123456789";
4 //SwapChar(s, 3, 6);
5 Console.Write(SwapChar(s, 3, 6).ToString());
6 }
7
8 static string SwapChar(string s, int p1, int p2)
9 {
10 if ((p1 == p2) || ((p1 < 0) || (p2 < 0))) return s;
11 if ((p1 >= s.Length) || (p2 >= s.Length)) return s;
12 char[] vChars = s.ToCharArray();
13 vChars[p1] = (char)(vChars[p2] | (vChars[p2] = vChars[p1]) & 0);
14 return new string(vChars);
15 }
1 static void Main()
2 {
3 string s = "123456789";
4 Console.Write(SwapChar(s, 3, 6).ToString());
5 }
6
7 public static string SwapChar(string str, int a, int b)
8 {
9 char[] newStr = str.ToCharArray();
10 newStr[a] = str[b];
11 newStr[b] = str[a];
12 return new string(newStr);
13 }
方法四:
2 {
3 string s = "123456789";
4 Console.Write(SwapChar(s, 3, 6).ToString());
5 }
6
7 public static string SwapChar(string str, int a, int b)
8 {
9 char[] newStr = str.ToCharArray();
10 newStr[a] = str[b];
11 newStr[b] = str[a];
12 return new string(newStr);
13 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Text.RegularExpressions;
5 namespace ConsoleApplication1
6 {
7 class Program
8 {
9 static void Main()
10 {
11 string s = "123456789";
12 Console.Write(SwapChar(s, 3, 6).ToString());
13 }
14
15 static string SwapChar(string s, int p1, int p2)
16 {
17 if (p1 > p2) { int p = p1; p1 = p2; p2 = p; }
18 return Regex.Replace(s, "^(.{" + p1 + "})(.)(.{" + (p2 - p1 - 1) + "})(.)(.*)$", "");
19 }
20
21 }
22 }
输出21个AAAAAAAAAAAAAAAAAAAAA的巧妙做法:
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Text.RegularExpressions;
5 namespace ConsoleApplication1
6 {
7 class Program
8 {
9 static void Main()
10 {
11 string s = "123456789";
12 Console.Write(SwapChar(s, 3, 6).ToString());
13 }
14
15 static string SwapChar(string s, int p1, int p2)
16 {
17 if (p1 > p2) { int p = p1; p1 = p2; p2 = p; }
18 return Regex.Replace(s, "^(.{" + p1 + "})(.)(.{" + (p2 - p1 - 1) + "})(.)(.*)$", "");
19 }
20
21 }
22 }
new构造器的理解
如果要你创建一个由21个"A"字符构成的字符串,你会怎么做?
string str = "AAAAAAAAAAAAAAAAAAAAA";//老实型
string str = new string('A', 21);//简单聪明
浙公网安备 33010602011771号