C#中等号左右的文本值交换

 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.

 

转载于:https://www.cnblogs.com/GIRLANDBOYS/p/7002435.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值