<code class="lang-c">/* * Created by SharpDevelop. * User: surface * Date: 04/11/2016 * Time: 21:43 * * To change this template use Tools | Options | Coding | Edit Standard Headers. */ using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.Threading; using System.Threading.Tasks; using System.Text; namespace mywinforms1234 { /// <summary> /// Description of MainForm. /// </summary> public partial class MainForm : Form { public MainForm() { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent(); // // TODO: Add constructor code after the InitializeComponent() call. // } void Button1Click(object sender, EventArgs e) { var arr_a = new byte[] { 0x80, 0x81, 0x82, 0x83 }; var arr_b = new byte[] { 0x81, 0x82 }; string a = Encoding.GetEncoding(28591).GetString(arr_a); string b = Encoding.GetEncoding(28591).GetString(arr_b); MessageBox.Show(a.Contains(b).ToString()); // 测试a是否包含b } } }</code>
<code class="lang-c">/* * Created by SharpDevelop. * User: surface * Date: 2016/5/6 * Time: 15:26 * * To change this template use Tools | Options | Coding | Edit Standard Headers. */ using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.Text; using System.IO; namespace encodingtest1 { /// <summary> /// Description of MainForm. /// </summary> public partial class MainForm : Form { public MainForm() { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent(); // // TODO: Add constructor code after the InitializeComponent() call. // } void Button1Click(object sender, EventArgs e) { // 示例文本 string sampletxt = "sssaaa\r\n我是中文测试一二三四五\r\n"; // 写入示例文本,注意.NET默认写入的文本有BOM File.WriteAllText("abc.txt", sampletxt, Encoding.UTF8); // 注意一定要将detectEncodingFromByteOrderMarks关掉,否则会受BOM影响而失效 string content = ""; using (StreamReader sr = new StreamReader("abc.txt", Encoding.GetEncoding(28591), false)) { content = sr.ReadToEnd(); MessageBox.Show(content); //content = content.Replace("\u00ef\u00bb\u00bf", ""); if (content.StartsWith("\u00ef\u00bb\u00bf")) // 如果有BOM的话 content = content.Substring(3); // 去掉bom MessageBox.Show(content); } // 重新写入修改过的内容 using (StreamWriter sw = new StreamWriter("abc.txt", false, Encoding.GetEncoding(28591))) { sw.Write(content); } // 再次测试有无BOM using (StreamReader sr = new StreamReader("abc.txt", Encoding.GetEncoding(28591), false)) { content = sr.ReadToEnd(); MessageBox.Show(content); } // 如果单纯是为了写入无BOM的UTF-8文件,不用这么麻烦,这样就可以了 File.WriteAllBytes("abc.txt", Encoding.UTF8.GetBytes(sampletxt)); // 再次测试有无BOM using (StreamReader sr = new StreamReader("abc.txt", Encoding.GetEncoding(28591), false)) { content = sr.ReadToEnd(); MessageBox.Show(content); } } } }</code>
[修改于 8年8个月前 - 2016/05/07 23:59:11]
时段 | 个数 |
---|---|
{{f.startingTime}}点 - {{f.endTime}}点 | {{f.fileCount}} |