• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
姑娘就是先生
博客园    首页    新随笔    联系   管理    订阅  订阅
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.

 

posted on 2017-06-13 17:25  姑娘就是先生  阅读(736)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3