常见几种字符串操作

1、取字符串长度

取字符串长度
1 string str="Relict";
2 int Len=str.Length;//得到字符串str的长度

2、字符串转为比特码

字符串转为比特码
1 byte[] bytStr = System.Text.Encoding.Default.GetBytes(str);
2 //然后可以得到比特长度
3 len = bytStr.Length;

3、字符串相加

字符串相加
1 Systrm.Text.StringBuilder sd = new Systrm.Text.StringBuilder();
2 sb.Append("Relict");
3 sb.Append("[email protected]");
4 //推荐使用上例,虽然和"+"等效,但性能更好一些
5 string str = "Relict" + "[email protected]";

4、截取字符串的一部分

语法:str.Substring(起始位置,截取字符串位数)

   例如:截取str="Relict";的前两位结果为"Re" 

1 str.Substring(0,2);

5、检查指定位置是否为空字符(正确性未知

语法:char.IsWhiteSpce(字符串,第几位);//位数从第0位开始

   例如:检查str="Relict [email protected]"的第六位是否是空字符

1 Response.Write(char.IsWhiteSpce(str,6));

   结果为True,第一个字符是0位,6就是第七个字符,正好是一个空格。

6、检查字符是否为标点符号

语法:char.IsPunctuation('字符');

   例如:检查'R'是否是标点符号

1 Response.Write(char.IsPunctuation('R'));

   结果为False

7、把字符转化为数字,查代码点。

语法:(int)'字符';

1 Response.Write((int)'中');

结果为:20013//注意是字符要用单引号

8、把数字转为字符,查代码代表的字符

语法:(char)数字代码

1 Response.Write((char)22269));

结果为:’国' 结果为字符一定要注意

9、清除字符串前后的空格

1 " Relict ".Trim();

10、替换字符串

1 Response.Write(str.Replace("e","e2012"));

结果为:Re2012lict

11、删除字符串最后一个字符的3种方法

方法一:也是最常用的Substring;

1 Response.Write(str.Substring(0,str.Length-1));

方法二:TrimEnd(params char[] trimChars) 从当前String对象中移除数组中指定的一组字符的所有尾部匹配项

1 Response.Write(str.TrimEnd('t'));

方法三:Remove删除字符串从指定位置到最后的所有字符

1 Response.Write(Str.Remove(Str.Length - 1));

结果为:Relic

12、Split的常见3种用法

用法一:用单个字符分割

用单个字符分割
1  Str = "Relict,Relict2012,[email protected]";
2 string[] StrArray = Str.Split(',');
3 //string[] StrArray = Str.Split(new char[]{','});这种写法也可以
4 foreach (string s in StrArray)
5 {
6 Response.Write(s.ToString() + "<br />");
7 }

用法二:用多个字符分割

用多个字符分割
1  Str = "[email protected]";
2 //得到以'k'和'+'分割的字符串数组
3 StrArray = Str.Split(new char[] { 'k', '+' });
4 foreach (string s in StrArray)
5 {
6 Response.Write(s.ToString() + "<br />");
7 }

用法三:用字符串分割

用字符串分割
1 Str = "Relict&#%#&Relict2012&#%#&[email protected]";
2 StrArray = Regex.Split(Str,"&#%#&",RegexOptions.IgnoreCase);
3 foreach (string s in StrArray)
4 {
5 Response.Write(s.ToString() + "<br />");
6 }

13、几种数字格式化

几种输出数字格式化
1 Response.Write(123456.ToString("n") + "<br />");//123,456.00
2 Response.Write(123456.ToString("C") + "<br />");//¥123,456.00
3 Response.Write(123456.ToString("e") + "<br />");//1.234560e+005
4 Response.Write(123456.ToString("f4") + "<br />");//123456.0000
5 Response.Write(123456.ToString("x") + "<br />");//1e240
6 Response.Write(123456.ToString("p") + "<br />");//12,345,600.00%

14、把123456789转换为12-345-6789的三种方法

方法一:格式化输出

 Response.Write(123456789.ToString("##-###-####"));

方法二: 指定位置插入字符串

1 Response.Write(123456789.ToString().Insert(5,"-").Insert(2,"-"));

方法三:正则表达式匹配

正则表达式匹配
1 //注意添加引用using System.Text.RegularExpressions;
2 Regex reg = new Regex(@"^(\d{2})(\d{3})(\d{4})$");
3 Response.Write(reg.Replace("123456789", "$1-$2-$3"));

15、输出21个A

方法一:for循环输出

for循环输出
1 StringBuilder str21A = new StringBuilder();
2 for (int i = 0; i < 21; i++)
3 {
4 str21A.Append("A");
5 }
6 Response.Write(str21A.ToString());

方法二:new string

1 Response.Write(new string('A',21));

16、获取随机数

获取随机数
1 Random r = new Random();
2 Response.Write("返回非负随机整数" + r.Next().ToString() + "<br />");
3 Response.Write("返回指定最大值(10)的非负随机整数:" + r.Next(10).ToString() + "<br />");
4 Response.Write("返回指定最大值(10)的非负随机整数:" + (r.Next() % 10).ToString() + "<br />");
5 Response.Write("返回指定范围(0-20)内的随机整数:" + r.Next(0, 20).ToString() + "<br />");
6 Response.Write("返回0.0-1.0内的随机数:" + r.NextDouble().ToString() + "<br />");

 17、Convert.ToInt32()、Int32.Parse()与Int32.TryParse()的比较

测试代码不写了,测试思路

定义string Mystr="123456";然后用三种方法分别转换,发现结果一样。

下来定义string Mystr=null;然后就会发现不同之处了

Convert.ToInt32()不抛出异常,返回值为0;

Int32.Parse()抛出异常;

Int32.TryParse()不抛出异常,但通过返回值是True还是Flase来判断解析是否成功。如果解析失败,返回值为Flase,则out调用的参数将会得到零值。

从性能上看,Int32.TryParse() > Int32.Parse() > Convert.ToInt32()

 

大部分CS代码
  1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using System.Text.RegularExpressions;
8 using System.Text;
9
10 namespace Dome
11 {
12 public partial class WebForm2 : System.Web.UI.Page
13 {
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 //================1==========================
17 string Str = "Relict";
18 int Len = Str.Length;
19 Response.Write(Str + "的长度为" + Len.ToString());
20 Response.Write("<br />");
21 //================2==========================
22 byte[] bytStr = System.Text.Encoding.Default.GetBytes(Str);
23 Len = bytStr.Length;
24 Response.Write(Str + "的比特长度为:" + Len.ToString());
25 Response.Write("<br />");
26 //===============5===========================
27 //Response.Write(char.IsWhiteSpce("Relict [email protected]", 6).ToString());
28 //Response.Write("<br />");
29 //===============6===========================
30 Response.Write(char.IsPunctuation('R').ToString());
31 Response.Write("<br />");
32 //===============7===========================
33 Response.Write((int)'');
34 Response.Write("<br />");
35 //===============8===========================
36 Response.Write((char)22269);
37 Response.Write("<br />");
38 //===============9===========================
39 Response.Write(" Relict 去除前后空格:" + " Relict ".Trim() + ";");
40 Response.Write("<br />");
41 //===============10===========================
42 Response.Write(Str.Replace("e", "e2012"));
43 Response.Write("<br />");
44 //===============11===========================
45 Response.Write(Str.Substring(0, Str.Length - 1));
46 Response.Write("<br />");
47 Response.Write(Str.TrimEnd('t'));
48 Response.Write("<br />");
49 Response.Write(Str.Remove(Str.Length - 1));
50 Response.Write("<br />");
51 //===============12===========================
52 Str = "Relict,Relict2012,[email protected]";
53 string[] StrArray = Str.Split(',');
54 //string[] StrArray = Str.Split(new char[]{','});这种写法也可以
55 foreach (string s in StrArray)
56 {
57 Response.Write(s.ToString() + "<br />");
58 }
59 Str = "[email protected]";
60 //得到以'k'和'+'分割的字符串数组
61 StrArray = Str.Split(new char[] { 'k', '+' });
62 foreach (string s in StrArray)
63 {
64 Response.Write(s.ToString() + "<br />");
65 }
66 Str = "Relict&#%#&Relict2012&#%#&[email protected]";
67 StrArray = Regex.Split(Str, "&#%#&", RegexOptions.IgnoreCase);
68 foreach (string s in StrArray)
69 {
70 Response.Write(s.ToString() + "<br />");
71 }
72 //===============13===========================
73 Response.Write(123456.ToString("n") + "<br />");//123,456.00
74 Response.Write(123456.ToString("C") + "<br />");//¥123,456.00
75 Response.Write(123456.ToString("e") + "<br />");//1.234560e+005
76 Response.Write(123456.ToString("f4") + "<br />");//123456.0000
77 Response.Write(123456.ToString("x") + "<br />");//1e240
78 Response.Write(123456.ToString("p") + "<br />");//12,345,600.00%
79 //===============14===========================
80 //格式化输出
81 Response.Write(123456789.ToString("##-###-####"));
82 Response.Write("<br />");
83 //指定位置插入字符串
84 Response.Write(123456789.ToString().Insert(5, "-").Insert(2, "-"));
85 Response.Write("<br />");
86 //正则表达式匹配
87 Regex reg = new Regex(@"^(\d{2})(\d{3})(\d{4})$");
88 Response.Write(reg.Replace("123456789", "$1-$2-$3"));
89 Response.Write("<br />");
90 //===============15===========================
91 StringBuilder str21A = new StringBuilder();
92 for (int i = 0; i < 21; i++)
93 {
94 str21A.Append("A");
95 }
96 Response.Write(str21A.ToString());
97 Response.Write("<br />");
98 Response.Write(new string('A', 21));
99 Response.Write("<br />");
100 //===============16===========================
101 Random r = new Random();
102 Response.Write("返回非负随机整数" + r.Next().ToString() + "<br />");
103 Response.Write("返回指定最大值(10)的非负随机整数:" + r.Next(10).ToString() + "<br />");
104 Response.Write("返回指定最大值(10)的非负随机整数:" + (r.Next() % 10).ToString() + "<br />");
105 Response.Write("返回指定范围(0-20)内的随机整数:" + r.Next(0, 20).ToString() + "<br />");
106 Response.Write("返回0.0-1.0内的随机数:" + r.NextDouble().ToString() + "<br />");
107
108 }
109 }
110 }

你可能感兴趣的:(字符串)