C#利用substring按指定长度分割字符串

  这几天学习分析声音的波形数据,接收到的是十六进制的数据,需要将数据转换成十进制再绘图,这个过程涉及到字符串的分割,正好可以促进自己对C#相关知识的学习。说到分割字符串,我首先想到的是Split,但根据本例分割要求无法直接使用,需要进行一些处理。通过比较,我觉得常用于截取字符串的substring函数可以较方便的解决该问题,故记录下来方便与大家交流、学习。(相信一定有更好的处理方法,希望各位不吝赐教)

一.该程序的主要目的/功能

原数据如下图所示(十六进制数据):

 

C#利用substring按指定长度分割字符串

原数据4byte表示一个数(即图中第一个数是7E51,第二个是75BA……),需要将原数据

指定长度(4byte)分割成新的字符串,然后转化为十进制的数:

C#利用substring按指定长度分割字符串

运行界面:

C#利用substring按指定长度分割字符串

/*左边文本框是十六进制的原数据,右边是按长度为4(byte)分割后转化得到的相应的十进制数*/

二.程序代码

C#利用substring按指定长度分割字符串

C#利用substring按指定长度分割字符串

1 using System; 2 using System.Text; 3 using System.Windows.Forms; 4 using System.IO; 5 using System.Text.RegularExpressions; 6 7 namespace StringOperate 8 { 9 public partial class Form1 : Form 10 { 11 public Form1() 12 { 13 InitializeComponent(); 14 } 15 16 /// <summary> 17 /// 去掉字符串中的空格、回车和换行,然后按指定长度进行分割 18 /// </summary> 19 /// <param>待处理的字符串</param> 20 /// <returns></returns> 21 static string[] strProcess(string str) 22 { 23 string strProcessed; 24 string pass = @"[\t\r\n\s]"; 25 strProcessed = Regex.Replace(str, pass, ""); //去掉、回车、换行、空格 26 int strLength = strProcessed.Length; //统计经处理后的字符长度 27 //判断待处理的字符串长度是否符合要求,并给出提示 28 if (strLength % 4 != 0) 29 { 30 int absenceNum = 4-strLength % 4; 31 string notification = $"注意:请检查数据是否完整!\r\n[提示:数据似乎缺少{absenceNum}位数字数符]"; 32 MessageBox.Show(notification, "Tips", MessageBoxButtons.OK, MessageBoxIcon.Information); 33 } 34 int byteLength = strProcessed.Length / 4; //统计分割后字符数组长度,数组中每个元素长度为四个字节 35 string[] strArrayH = new string[byteLength]; //存放十六进制字符串 36 int[] dateDecimal = new int[byteLength]; //存放转换后的十进制数据 37 //将字符串分割为长度为4的字符数组 38 for (int i=0;i<(byteLength);i=i+1) 39 { 40 try 41 { 42 strArrayH[i]= strProcessed.Substring(4*i,4);//i-起始位置,4-子串长度 43 } 44 catch (Exception e) 45 { 46 MessageBox.Show(e.ToString(),"异常提示",MessageBoxButtons.OK,MessageBoxIcon.Warning); 47 continue; 48 } 49 } 50 return strArrayH; 51 } 52 53 private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) 54 { 55 string mydate; 56 OpenFileDialog Ofdlg = new OpenFileDialog(); 57 openFileDialog1.Filter = "Word工作簿97-2003(*.doc)|*.doc|txt files (*.txt)|*.txt|All files (*.*)|*.*"; 58 openFileDialog1.FileName = "default"; 59 openFileDialog1.FilterIndex = 2; 60 openFileDialog1.Title = "请选择待处理的数据文本"; //对话框的标题 61 62 if (openFileDialog1.ShowDialog()==DialogResult.OK) 63 { 64 FileStream myfile = new FileStream(openFileDialog1.FileName,FileMode.Open,FileAccess.Read); 65 //使用System.Text.Encoding.Defaul告诉StreamReader采用目前操作系统的编码,避免出现乱码 66 StreamReader SR = new StreamReader(myfile,Encoding.Default); 67 mydate = SR.ReadToEnd(); 68 try 69 { 70 textBox1.Text = mydate; 71 SR.Close(); 72 } 73 catch (Exception ex) 74 { 75 MessageBox.Show("打开文件出错:" + ex.Message); 76 } 77 } 78 } 79 80 //将文本按指定长度分割 81 private void 分割字符串ToolStripMenuItem_Click(object sender, EventArgs e) 82 { 83 textBox2.Text = ""; 84 string myContentBefore; 85 string[] myContentAfter; 86 myContentBefore = textBox1.Text.Trim(); 87 myContentAfter = strProcess(myContentBefore); 88 int[] dateDecimal = new int[myContentAfter.Length]; 89 for (int i = 0; i < myContentAfter.Length; i++) 90 { 91 try 92 { 93 dateDecimal[i] = Convert.ToInt32(myContentAfter[i], 16); 94 } 95 catch (Exception ex) 96 { 97 MessageBox.Show(ex.ToString(),"提示"); 98 continue; 99 } 100 finally 101 { 102 /* 103 * textBox2.Text = (textBox2.Text + " " + dateDecimal[i].ToString()).Trim(); 104 * textBox2.Text += dateDecimal[i].ToString() + " "; 105 * 当数据较多时,上面两种给textBox的赋值方式响应较慢,程序可能出现假死 106 * 需要频繁更新TextBox(追加文本)时,AppendText能够稳定的即时更新,而且高效 107 */ 108 textBox2.AppendText(dateDecimal[i].ToString()+" "); 109 } 110 } 111 textBox2.Text = textBox2.Text.TrimEnd(); 112 113 } 114 115 //清空textbox1的内容 116 private void 清除text1ToolStripMenuItem_Click(object sender, EventArgs e) 117 { 118 textBox1.Text = ""; 119 } 120 121 //清空textbox2的内容 122 private void 清除text2ToolStripMenuItem_Click(object sender, EventArgs e) 123 { 124 textBox2.Text = ""; 125 } 126 127 private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) 128 { 129 StreamWriter mySteam; 130 SaveFileDialog Sfdlg = new SaveFileDialog(); 131 saveFileDialog1.Filter= " Word文档97-2003(*.doc)|*.doc|txt files(*.txt)|*.txt|Excel工作簿97-2003(*.xls)|*.xls|All files(*.*)|*.*"; 132 saveFileDialog1.FilterIndex = 2; //设置默认文件类型 133 saveFileDialog1.FileName = "default1"; //设置文件的默认名称 134 saveFileDialog1.RestoreDirectory = true; //记忆上次打开位置 135 if (saveFileDialog1.ShowDialog()== DialogResult.OK) 136 { 137 mySteam = new StreamWriter(saveFileDialog1.FileName); 138 mySteam.Write(textBox2.Text.Trim()); 139 //使用Flush()方法将所有信息从基础缓冲区移动到其目标或清除缓冲区,或者同时执行这两种操作 140 mySteam.Flush(); 141 mySteam.Close(); 142 } 143 } 144 } 145 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zygxxd.html