导航

C#笔记-01:几个常用的字符串处理函数

Posted on 2020-08-06 23:01  ErgoCogito  阅读(212)  评论(0编辑  收藏  举报

字符串截取,插入,替换常用函数(复制自https://www.cnblogs.com/yangwujun/p/5565941.html):

  1 using System;
  2 using System.Collections;
  3 using System.Configuration;
  4 using System.Data;
  5 using System.Linq;
  6 using System.Web;
  7 using System.Web.Security;
  8 using System.Web.UI;
  9 using System.Web.UI.HtmlControls;
 10 using System.Web.UI.WebControls;
 11 using System.Web.UI.WebControls.WebParts;
 12 using System.Xml.Linq;
 13 
 14 namespace demo
 15 {
 16     public partial class _string : System.Web.UI.Page
 17     {
 18         protected void Page_Load(object sender, EventArgs e)
 19         {
 20             if (IsPostBack == false)
 21             {
 22                 string a = "123a4a4b5b6";
 23                 TextBox1.Text = a;
 24             }
 25             
 26         }
 27 
 28         protected void Button1_Click(object sender, EventArgs e)
 29         {
 30             string b = TextBox1.Text;
 31 
 32             ////截取前两位 123a4a4b5b6   12
 33             //string c = b.Substring(0, 2);
 34             //TextBox1.Text = c;
 35 
 36             ////截取后两位  123a4a4b5b6   56
 37             //string c = b.Substring(b.Length - 2, 2);
 38             //TextBox1.Text = c;
 39 
 40             ////截取最后一个字符“a”后面的3位字符 123a4a4b5b6     a4b
 41             //string c = b.Substring(b.LastIndexOf("a"), 3);
 42             //TextBox1.Text = c;
 43 
 44             ////截取最后一个字符“a”后面所有的字符  123a4a4b5b6     a4b5b6
 45             //string c = b.Substring(b.LastIndexOf("a"));
 46             //TextBox1.Text = c;
 47 
 48             ////截取最后一个字符“a”前面所有的字符  123a4a4b5b6    123a4 
 49             //string c = b.Remove(b.LastIndexOf("a"));
 50             //TextBox1.Text = c;
 51             //// 截取最后一个字符“a”前面所有的字符  123a4a4b5b6    123a4 
 52             //string c = b.Substring(0, b.LastIndexOf("a"));
 53             //TextBox1.Text = c;
 54 
 55             ////截取最后一个字符“a”前面的3位字符  123a4a4b5b6    123a4    3a4
 56             //string c = b.Remove(b.LastIndexOf("a"));
 57             //string d = c.Remove(0, c.Length - 3);
 58             //TextBox1.Text = d;
 59             //// 截取最后一个字符“a”前面的3位字符  123a4a4b5b6     3a4    
 60             //string c = b.Substring(2,b.LastIndexOf("a")-2);
 61             //TextBox1.Text = c;
 62 
 63             ////截取第一个字符"b"前面所有的字符  123a4a4b5b6  123a4a4
 64             //int index = b.IndexOf("b");
 65             //string c = b.Substring(0,index);
 66             //TextBox1.Text = c;
 67 
 68             ////截取第一个字符"b"前面的3位字符  123a4a4b5b6   4a4
 69             //string c = b.Substring(0, b.IndexOf("b"));
 70             //string d = c.Substring(c.Length - 3, 3);
 71             //TextBox1.Text = d;
 72             ////截取第一个字符"b"前面的3位字符  123a4a4b5b6   4a4
 73             //string c = b.Substring(b.IndexOf("b")-3,3);
 74             //TextBox1.Text = c;
 75 
 76             ////截取第一个字符"b"后面所有的字符  123a4a4b5b6   b5b6
 77             //int index = b.IndexOf("b");
 78             //string c = b.Substring(index,b.Length-index);
 79             //TextBox1.Text = c;
 80             //////截取第一个字符"b"后面3位字符  123a4a4b5b6   b5b
 81             //int index = b.IndexOf("b");
 82             //string c = b.Substring(index, 3);
 83             //TextBox1.Text = c;
 84 
 85 
 86 
 87 
 88 
 89 
 90 
 91 
 92             ////移除从第三位开始的3个字符 123a4a4b5b6   1234b5b6
 93             //string c = b.Remove(3, 3);
 94             //TextBox1.Text = c;
 95             ////移除字符串中的所有字符b;  123a4a4b5b6     123a4a456
 96             //string c = b.Replace("b", "");
 97             //TextBox1.Text = c;
 98 
 99             ////移除字符串中的第一个字符a   123a4a4b5b6  1234a4b5b6  
100             //int index = b.IndexOf("a",1);
101             //b=b.Remove(index, 1);
102             //TextBox1.Text = b;
103 
104             ////移除字符串中的第一个字符a的后两位   123a4a4b5b6  123a4b5b6  
105             //int index = b.IndexOf("a", 1);
106             //b = b.Remove(index, 2);
107             //TextBox1.Text = b;
108 
109             ////移除字符串中的第二个字符a   123a4a4b5b6   123a4b5b6
110             //int index = b.IndexOf("a", 2);
111             //b = b.Remove(index, 2);
112             //TextBox1.Text = b;
113 
114             ////移除字符串中的第二个字符a的后两位   123a4a4b5b6    123a4b5b6 
115             //int index = b.IndexOf("a", 1);
116             //b = b.Remove(index, 2);
117             //TextBox1.Text = b;
118 
119             ////移除字符串中最后一个字符b   123a4a4b5b6      123a4a4b56 
120             //int index = b.LastIndexOf("b");     
121             //string c = b.Remove(b.LastIndexOf("b"),1);
122             //TextBox1.Text = c;
123 
124             ////移除字符串中最后一个字符b的后两位   123a4a4b5b6      123a4a4b5 
125             //int index = b.LastIndexOf("b");
126             //string c = b.Remove(b.LastIndexOf("b"), 2);
127             //TextBox1.Text = c;
128 
129 
130 
131             ////替换字符串中所有的字符“a”,为“b”; 123a4a4b5b6     123b4b4b5b6
132             //string c = b.Replace("a", "b");
133             //TextBox1.Text = c;
134 
135             ////替换字符中的第一个字符a为R  123a4a4b5b6    123R4a4b5b6
136             //int index = b.IndexOf("a");
137             //string c= b.Remove(index, 1).Insert(index, "R");
138             //TextBox1.Text = c;
139 
140             ////替换字符中的最后一个字符a为R  123a4a4b5b6    123a4R4b5b6
141             //int index = b.LastIndexOf("a");
142             //string c = b.Remove(index, 1).Insert(index, "R");
143             //TextBox1.Text = c;
144 
145 
146 
147             ////插入I在第一个a之前  123a4a4b5b6  123Ia4a4b5b6
148             //int index = b.IndexOf("a");
149             //string c = b.Insert(index, "I");
150             //TextBox1.Text = c;
151 
152             ////插入I在第一个a之后  123a4a4b5b6   123aI4a4b5b6
153             //int index = b.IndexOf("a");
154             //string c = b.Insert(index+1, "I");
155             //TextBox1.Text = c;
156 
157             ////插入I在最后一个a之前  123a4a4b5b6  123a4Ia4b5b6
158             //int index = b.LastIndexOf("a");
159             //string c = b.Insert(index, "I");
160             //TextBox1.Text = c;
161 
162             //插入I在最后一个a之后  123a4a4b5b6  123a4aI4b5b6
163             int index = b.LastIndexOf("a");
164             string c = b.Insert(index + 1, "I");
165             TextBox1.Text = c;
166 
167         }
168     }
169 }

 

读取字符常用函数

Read()函数:

Read()返回int ReadLine()返回String
这个返回的值是你输入的第一个字符的UNICODE码,不管你输入的是多少个字符,他只返回第一个字符
int c=Console.Read();
int d=Console.Read();
Console.WriteLine(c+"+"+d);
测试用例是 输入ww
那么输出结果是: 119+119
 W的unicode码是119,由此可见Read方法确实如上所述,并且不会等待你再次输入直接显示结果。
 
ReadLine()函数:
string c=Console.ReadLine();
string d=Console.ReadLine();
Console.WriteLine(c+"+"+d);
return   0;  

测试用例还是ww

输出结果 ww+ww

那么为什么会是这种表现呢?在做测试时可以看到它等待你输入回车,然后光标在下一行闪烁等待你再次输入,再次按下回车屏幕才会显示出结果并且结果也和上次表现不同,它的返回类型是string类型,并且输入的是什么输出的表现还是什么。
Readline是读到一个回车为止,以回车为分界线。
当你需要读取一行行的字符串时,这是一个比较合适的方法。
 

ReadExisting()函数:

用于读取串口全部的数据,不同于ReadLine()函数会以换行符或者回车为分界,初学时,曾经因为这两者的区别费了好大劲。。。

 

Parse()函数:

可实现不同类型的变量之间的转换,比如将字符串str转换为32位的整型。

int test = Int32.Parse(str);