1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace Change 12 { 13 public partial class FrmChange : Form 14 { 15 public FrmChange() 16 { 17 InitializeComponent(); 18 } 19 20 private void btnChange_Click(object sender, EventArgs e) 21 { 22 //获取到第一个文本框的文本内容 23 string top = txtTop.Text.Trim(); 24 //判断第一个文本框里面是否有=号 25 int count1 = top.Count(c1=>c1=='='); 26 //判断第一个文本框里面是否有分号 27 int count2 = top.Count(c2=>c2==';'); 28 //返回包含此字符串string的数组,以分号分割, 29 string[] arr1 = top.Split(';'); 30 string[] arr2=new string[arr1.Length]; 31 if (count1 > 0 && count2 > 0) 32 { 33 //截取每个分号前的字符串 34 for (int i = 0; i < arr1.Length - 1; i++) 35 { 36 //获取分号存在的下标 37 int index = top.IndexOf(";"); 38 //获取每个分号的字符串 39 string fh = top.Substring(0, index); 40 arr2[i] = fh; 41 top = top.Substring(index + 1); 42 } 43 44 for (int i = 0; i < arr2.Length - 1; i++) 45 { 46 string txt = arr2[i].Trim(); 47 //获取=号存在的下标 48 int index = txt.IndexOf("="); 49 //获取=号左边的字符串 50 string left = txt.Substring(0, index); 51 //获取=号右边的字符串 52 string right = txt.Substring(index + 1); 53 arr2[i] = right + "=" + left; 54 MessageBox.Show(arr2[i], "提示"); 55 } 56 57 //遍历arr2数组 并且给下面的文本框赋值 58 for (int i = 0; i < arr2.Length - 1; i++) 59 { 60 txtDown.Text += arr2[i] + ";\r\n"; 61 } 62 }else { 63 MessageBox.Show("文本框中未输入分号或者等号","女神提示"); 64 } 65 66 } 67 } 68 }
代码运行图:
1.
2.
3.