|
|
Copyright © 2003-2010 By Suthep Sangvirotjanaphat |
|
|
|
Mobile: 089-967-2200, 081-915-7816 Phone: 0-2992-4877 Fax: 0-2992-4878 Fast Contact Us |
GreatFriends.Biz Community |
Custom Search
|
public string textFileReader(string pathFileName)
{
string line;
StreamReader fs;
try{
fs = new StreamReader(pathFileName);
line = fs.ReadToEnd();
/* อ่าน Encode จาก String ที่อ่านมาได้จาก text file */
Encoding encodeSource = Encoding.GetEncoding(fs.CurrentEncoding.CodePage);
fs.Close();
//* ............ */
Encoding systemEncode = Encoding.Default;
Encoding targetEncode = encodeSource;
/* สั่ง getbyte array จาก string ที่เราอ่านมา */ byte[] srcData = systemEncode.GetBytes( line ); byte[] dstData; /* ถ้าเป้น Encode ต่างกัน windows และ text file ให้ Convert byte array ไปเป็น windows encode */ if( targetEncode != systemEncode )dstData = Encoding.Convert( systemEncode, targetEncode, srcData );
elsedstData = srcData;
/* convert bytearray ไปเป็น string ด้วย windows encode */ return targetEncode.GetString(dstData);}
catch(Exception ex){ throw new IOException("cannot find " + pathFileName,ex);}
}
[VB.NET CODE]
Public Function textFileReader(ByVal pathFileName As String) As String
fs =
New StreamReader(pathFileName)line = fs.ReadToEnd
'/* อ่าน Encode จาก String ที่อ่านมาได้จาก text file */ Dim encodeSource As Encoding = Encoding.GetEncoding(fs.CurrentEncoding.CodePage)fs.Close()
Dim systemEncode As Encoding = Encoding.Default Dim targetEncode As Encoding = encodeSource '/* สั่ง getbyte array จาก string ที่เราอ่านมา */ Dim srcData As Byte() = systemEncode.GetBytes(line) Dim dstData As Byte() '/* ถ้าเป้น Encode ต่างกัน windows และ text file ให้ Convert byte array ไปเป็น windows encode */ If (targetEncode.CodePage <> systemEncode.CodePage) ThendstData = Encoding.Convert(systemEncode, targetEncode, srcData)
ElsedstData = srcData
End If Return targetEncode.GetString(dstData) Catch ex As Exception Throw New IOException("cannot find " & pathFileName, ex) End Try End Function|
|
Nine (นาย) |
วันที่ส่ง: 4 ม.ค. 49 01:54 GMT+7 วันที่ปรับล่าสุด: 5 ม.ค. 49 01:05 GMT+7 |
/* ถ้าเป้น Encode ต่างกัน windows และ text file ให้ Convert byte array ไปเป็น Text file encode */
if( targetEncode != systemEncode ) ........
|
|
Nine (นาย) |
วันที่ส่ง: 4 ม.ค. 49 04:06 GMT+7 วันที่ปรับล่าสุด: 4 ม.ค. 49 04:06 GMT+7 |
|
|
Nine (นาย) |
วันที่ส่ง: 4 ม.ค. 49 04:18 GMT+7 วันที่ปรับล่าสุด: 4 ม.ค. 49 04:18 GMT+7 |
ซะละ Regex rexLine = new Regex("[ก-ฮ]");
rexLine.IsMatch("ชุดสตริงที่ต้องการตรวจ") จะพบอักษร ก-ฮ ก็จะ Return ture ออกมาครับ
rexLine.IsMatch("This String for check") ไม่พบอักษร ก-ฮ ก็จะ Return flase ออกมาครับ
Reference สอน Regular Expression เป็น PHP แต่หลักการเดียวกันครับ
http://www.thaijobmarket.com/regularex.html
|
|
Nine (นาย) |
วันที่ส่ง: 5 ม.ค. 49 01:05 GMT+7 วันที่ปรับล่าสุด: 5 ม.ค. 49 01:05 GMT+7 |
|
เรียน .NET/OOAD กับ อ.สุเทพ (surrealist) Microsoft Most Valuable Professional (MVP): |
![]() ลงทะเบียนเรียน |