Hàm Bỏ Dấu Tiếng Việt Trong C - Lập Trình .NET

Trang

  • C#
  • ASP.NET
  • HTML
  • Javascript
  • Angular
  • MS SQL Server
  • Data Access
  • Azure
  • Must Have Tools
  • Interview Questions

Thứ Tư, 7 tháng 11, 2012

Hàm bỏ dấu tiếng việt trong C#

Bỏ dấu bằng cách replace các ký tự có dấu về ký tự không dấu tương ứng:
  1. public static string RemoveUnicode(string text)
  2. {
  3. string[] arr1 = new string[] { "á", "à", "ả", "ã", "ạ", "â", "ấ", "ầ", "ẩ", "ẫ", "ậ", "ă", "ắ", "ằ", "ẳ", "ẵ", "ặ",
  4. "đ",
  5. "é","è","ẻ","ẽ","ẹ","ê","ế","ề","ể","ễ","ệ",
  6. "í","ì","ỉ","ĩ","ị",
  7. "ó","ò","ỏ","õ","ọ","ô","ố","ồ","ổ","ỗ","ộ","ơ","ớ","ờ","ở","ỡ","ợ",
  8. "ú","ù","ủ","ũ","ụ","ư","ứ","ừ","ử","ữ","ự",
  9. "ý","ỳ","ỷ","ỹ","ỵ",};
  10. string[] arr2 = new string[] { "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
  11. "d",
  12. "e","e","e","e","e","e","e","e","e","e","e",
  13. "i","i","i","i","i",
  14. "o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o",
  15. "u","u","u","u","u","u","u","u","u","u","u",
  16. "y","y","y","y","y",};
  17. for (int i = 0; i < arr1.Length; i++)
  18. {
  19. text = text.Replace(arr1[i], arr2[i]);
  20. text = text.Replace(arr1[i].ToUpper(), arr2[i].ToUpper());
  21. }
  22. return text;
  23. }
Viết theo kiểu Extension:
  1. public static string NonUnicode(this string text)
  2. {
  3. string[] arr1 = new string[] { "á", "à", "ả", "ã", "ạ", "â", "ấ", "ầ", "ẩ", "ẫ", "ậ", "ă", "ắ", "ằ", "ẳ", "ẵ", "ặ",
  4. "đ",
  5. "é","è","ẻ","ẽ","ẹ","ê","ế","ề","ể","ễ","ệ",
  6. "í","ì","ỉ","ĩ","ị",
  7. "ó","ò","ỏ","õ","ọ","ô","ố","ồ","ổ","ỗ","ộ","ơ","ớ","ờ","ở","ỡ","ợ",
  8. "ú","ù","ủ","ũ","ụ","ư","ứ","ừ","ử","ữ","ự",
  9. "ý","ỳ","ỷ","ỹ","ỵ",};
  10. string[] arr2 = new string[] { "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
  11. "d",
  12. "e","e","e","e","e","e","e","e","e","e","e",
  13. "i","i","i","i","i",
  14. "o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o",
  15. "u","u","u","u","u","u","u","u","u","u","u",
  16. "y","y","y","y","y",};
  17. for (int i = 0; i < arr1.Length; i++)
  18. {
  19. text = text.Replace(arr1[i], arr2[i]);
  20. text = text.Replace(arr1[i].ToUpper(), arr2[i].ToUpper());
  21. }
  22. return text;
  23. }
Test:
  1. string test1 = "hành trang lập trình chấm cơm".NonUnicode();
  2. string test2 = Functions.RemoveUnicode("hành trang lập trình chấm cơm");

3 nhận xét :

  1. Unknownlúc 21:54 10 tháng 9, 2014

    Thank you very much

    Trả lờiXóaTrả lời
      Trả lời
  2. Unknownlúc 22:29 5 tháng 9, 2017

    hay lắm, thanks

    Trả lờiXóaTrả lời
      Trả lời
  3. okdd ddd dlúc 23:59 10 tháng 8, 2018

    Bạn có thể sử dụng Regex để viết ngắn gọn hơn.static Regex ConvertToUnsign_rg = null; public static string ConvertToUnsign(string strInput) { if (ReferenceEquals(ConvertToUnsign_rg, null)) { ConvertToUnsign_rg = new Regex("\\p{IsCombiningDiacriticalMarks}+"); } var temp = strInput.Normalize(NormalizationForm.FormD); return ConvertToUnsign_rg.Replace(temp, string.Empty).Replace("đ", "d").Replace("Đ", "D").ToLower(); }https://laptrinhvb.net

    Trả lờiXóaTrả lời
      Trả lời
Thêm nhận xétTải thêm... Bài đăng Mới hơn Bài đăng Cũ hơn Trang chủ Đăng ký: Đăng Nhận xét ( Atom )

Tìm kiếm Blog này

GitHubs

Star EntityFrameworkCore.SqlServer.SimpleBulks Star Practical.CleanArchitecture

References

dotnet - YouTube ASP.NET MSDN – Microsoft Developer Network Microsoft Docs .NET Blog Azure SQL Dev Blog Azure Cosmos DB Blog Microsoft Entra Identity Platform Visual Studio Blog Semantic Kernel Azure DevOps W3Schools Online Web Tutorials TIOBE Index DB-Engines Ranking Azure Updates Azure Blog

Nhãn

  • .NET Framework ( 4 )
  • 7-Zip ( 1 )
  • ASP.NET ( 15 )
  • ASP.NET Controls ( 1 )
  • ASP.NET Core ( 1 )
  • ASP.NET MVC ( 3 )
  • AsymmetricAlgorithm ( 1 )
  • Azure ( 2 )
  • Azure Active Directory ( 1 )
  • Babel ( 1 )
  • Bower ( 1 )
  • C# ( 59 )
  • Compression ( 1 )
  • Config ( 5 )
  • Cryptography ( 8 )
  • CSS ( 1 )
  • CSS Specificity ( 1 )
  • Data Access ( 1 )
  • DataReader ( 1 )
  • DateTime ( 4 )
  • delegate ( 1 )
  • Dictionary ( 1 )
  • Docker ( 1 )
  • Docker for Windows ( 1 )
  • EF Core ( 1 )
  • Entity Framework ( 6 )
  • Entity Framework Tools for Visual Studio ( 1 )
  • EPPlus ( 3 )
  • Excel ( 4 )
  • Execution Plan ( 2 )
  • FTP ( 1 )
  • Gulp ( 4 )
  • HashAlgorithm ( 2 )
  • HashSet ( 1 )
  • HMAC ( 1 )
  • HTML ( 5 )
  • IIS ( 3 )
  • javascript ( 5 )
  • JS Transpiler ( 1 )
  • Katana ( 1 )
  • KeyedHashAlgorithm ( 1 )
  • LINQ ( 3 )
  • LINQ To XML ( 1 )
  • Linux ( 1 )
  • log4net ( 1 )
  • Logging ( 1 )
  • Maintenance Plan ( 1 )
  • Multi Thread ( 1 )
  • MVC ( 1 )
  • MVP ( 1 )
  • NCalc ( 1 )
  • Network ( 4 )
  • NoSQL ( 1 )
  • Notepad++ ( 1 )
  • NPM ( 1 )
  • Nuget ( 1 )
  • OAuth ( 1 )
  • OctoPack ( 1 )
  • Office ( 3 )
  • Operator ( 2 )
  • OS ( 1 )
  • OWIN ( 2 )
  • Performance ( 2 )
  • Powershell ( 2 )
  • Print ( 1 )
  • RabbitMQ ( 5 )
  • Regex ( 4 )
  • ReportViewer ( 1 )
  • SMTP ( 1 )
  • Socket ( 1 )
  • SQL server ( 57 )
  • SQL Tutorial ( 4 )
  • SqlBulkCopy ( 1 )
  • SSIS ( 1 )
  • SSRS ( 1 )
  • SVN ( 2 )
  • SymmetricAlgorithm ( 1 )
  • T-SQL ( 18 )
  • TcpClient ( 1 )
  • TransactionScope ( 1 )
  • Unit Test ( 1 )
  • Visual Studio ( 2 )
  • WCF ( 1 )
  • Web API 2 ( 1 )
  • Webbrowser ( 1 )
  • Webbrowser Control ( 1 )
  • Webservice ( 1 )
  • What's new ( 1 )
  • Windows ( 3 )
  • Windows Form ( 2 )
  • Windows Server ( 1 )

Lưu trữ Blog

  • ▼  2012 ( 60 )
    • ▼  tháng 11 ( 8 )
      • Gọi hàm Javascript bằng webbrowser control C#
      • Cấu hình thông báo lỗi 404 Directory not found (Kh...
      • Lấy thông tin dung lượng phân vùng ổ cứng của máy ...
      • Tìm thứ 2 và thứ 7 trong tuần của 1 ngày bất kỳ
      • Hàm bỏ dấu tiếng việt trong C#
      • Cách làm hiệu ứng trượt khi mở Form C# Windows Form
      • Kiểm tra Hệ Điều Hành 64bit bằng C#
      • Nén và giải nén File trong C# .NET 4.5

Giới thiệu về tôi

Ảnh của Tôi Phong Nguyen Xem hồ sơ hoàn chỉnh của tôi

.NET

Đang tải...

Azure SQL Devs’ Corner

Đang tải...

Azure Cosmos DB Blog

Đang tải...

Microsoft Entra Identity Platform

Đang tải...

Visual Studio Blog

Đang tải...

Semantic Kernel

Đang tải...

Andrew Lock | .NET Escapades

Đang tải...

Meziantou's blog

Đang tải...

Từ khóa » Hàm Bỏ Dấu Tiếng Việt C#