vb.net学习笔记--字符串的截取、移除、替换、插入

.NET string字符串的截取、移除、替换、插入

string b = TextBox1.Text;
    截取
            ////截取前两位 123a4a4b5b6   12
            //string c = b.Substring(0, 2);
            //TextBox1.Text = c;

            ////截取后两位  123a4a4b5b6   56
            //string c = b.Substring(b.Length - 2, 2);
            //TextBox1.Text = c;

            ////截取最后一个字符“a”后面的3位字符 123a4a4b5b6     a4b
            //string c = b.Substring(b.LastIndexOf("a"), 3);
            //TextBox1.Text = c;

            ////截取最后一个字符“a”后面所有的字符  123a4a4b5b6     a4b5b6
            //string c = b.Substring(b.LastIndexOf("a"));
            //TextBox1.Text = c;

            ////截取最后一个字符“a”前面所有的字符  123a4a4b5b6    123a4 
            //string c = b.Remove(b.LastIndexOf("a"));
            //TextBox1.Text = c;
            //// 截取最后一个字符“a”前面所有的字符  123a4a4b5b6    123a4 
            //string c = b.Substring(0, b.LastIndexOf("a"));
            //TextBox1.Text = c;

            ////截取最后一个字符“a”前面的3位字符  123a4a4b5b6    123a4    3a4
            //string c = b.Remove(b.LastIndexOf("a"));
            //string d = c.Remove(0, c.Length - 3);
            //TextBox1.Text = d;
            //// 截取最后一个字符“a”前面的3位字符  123a4a4b5b6     3a4    
            //string c = b.Substring(2,b.LastIndexOf("a")-2);
            //TextBox1.Text = c;

            ////截取第一个字符"b"前面所有的字符  123a4a4b5b6  123a4a4
            //int index = b.IndexOf("b");
            //string c = b.Substring(0,index);
            //TextBox1.Text = c;

            ////截取第一个字符"b"前面的3位字符  123a4a4b5b6   4a4
            //string c = b.Substring(0, b.IndexOf("b"));
            //string d = c.Substring(c.Length - 3, 3);
            //TextBox1.Text = d;
            ////截取第一个字符"b"前面的3位字符  123a4a4b5b6   4a4
            //string c = b.Substring(b.IndexOf("b")-3,3);
            //TextBox1.Text = c;

            ////截取第一个字符"b"后面所有的字符  123a4a4b5b6   b5b6
            //int index = b.IndexOf("b");
            //string c = b.Substring(index,b.Length-index);
            //TextBox1.Text = c;
            //////截取第一个字符"b"后面3位字符  123a4a4b5b6   b5b
            //int index = b.IndexOf("b");
            //string c = b.Substring(index, 3);
            //TextBox1.Text = c;             

    移除
            ////移除从第三位开始的3个字符 123a4a4b5b6   1234b5b6
            //string c = b.Remove(3, 3);
            //TextBox1.Text = c;
            ////移除字符串中的所有字符b;  123a4a4b5b6     123a4a456
            //string c = b.Replace("b", "");
            //TextBox1.Text = c;

            ////移除字符串中的第一个字符a   123a4a4b5b6  1234a4b5b6  
            //int index = b.IndexOf("a",1);
            //b=b.Remove(index, 1);
            //TextBox1.Text = b;

            ////移除字符串中的第一个字符a的后两位   123a4a4b5b6  123a4b5b6  
            //int index = b.IndexOf("a", 1);
            //b = b.Remove(index, 2);
            //TextBox1.Text = b;

            ////移除字符串中的第二个字符a   123a4a4b5b6   123a4b5b6
            //int index = b.IndexOf("a", 2);
            //b = b.Remove(index, 2);
            //TextBox1.Text = b;

            ////移除字符串中的第二个字符a的后两位   123a4a4b5b6    123a4b5b6 
            //int index = b.IndexOf("a", 1);
            //b = b.Remove(index, 2);
            //TextBox1.Text = b;

            ////移除字符串中最后一个字符b   123a4a4b5b6      123a4a4b56 
            //int index = b.LastIndexOf("b");     
            //string c = b.Remove(b.LastIndexOf("b"),1);
            //TextBox1.Text = c;

            ////移除字符串中最后一个字符b的后两位   123a4a4b5b6      123a4a4b5 
            //int index = b.LastIndexOf("b");
            //string c = b.Remove(b.LastIndexOf("b"), 2);
            //TextBox1.Text = c;

    替换
            ////替换字符串中所有的字符“a”,为“b”; 123a4a4b5b6     123b4b4b5b6
            //string c = b.Replace("a", "b");
            //TextBox1.Text = c;

            ////替换字符中的第一个字符a为R  123a4a4b5b6    123R4a4b5b6
            //int index = b.IndexOf("a");
            //string c= b.Remove(index, 1).Insert(index, "R");
            //TextBox1.Text = c;

            ////替换字符中的最后一个字符a为R  123a4a4b5b6    123a4R4b5b6
            //int index = b.LastIndexOf("a");
            //string c = b.Remove(index, 1).Insert(index, "R");
            //TextBox1.Text = c;

    插入
            ////插入I在第一个a之前  123a4a4b5b6  123Ia4a4b5b6
            //int index = b.IndexOf("a");
            //string c = b.Insert(index, "I");
            //TextBox1.Text = c;

            ////插入I在第一个a之后  123a4a4b5b6   123aI4a4b5b6
            //int index = b.IndexOf("a");
            //string c = b.Insert(index+1, "I");
            //TextBox1.Text = c;

            ////插入I在最后一个a之前  123a4a4b5b6  123a4Ia4b5b6
            //int index = b.LastIndexOf("a");
            //string c = b.Insert(index, "I");
            //TextBox1.Text = c;

            //插入I在最后一个a之后  123a4a4b5b6  123a4aI4b5b6
            int index = b.LastIndexOf("a");
            string c = b.Insert(index + 1, "I");
            TextBox1.Text = c;

你可能感兴趣的:(学习笔记)