|
|
|
當前的位置 >> 返回首頁 >> 解決方案 |
|
軟件開發(fā)截取字符串 |
|
發(fā)布者:西安軟件公司 發(fā)布時間:2020/9/29 閱讀:次 |
|
protected void Button1_Click(object sender, EventArgs e) { 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; }
|
|