Bỏ dấu tiếng việt trong C++

By | 13/05/2018

Bắt nguồn từ bài viết này, nay mình có việc phải bỏ dấu tiếng Việt trong C++ nên share  thêm đoạn code bỏ dấu tiếng Việt vẫn là sử dụng hình thức replace.

Chú ý: Để biên dịch được đoạn code dưới đây, bạn cần trình biên dịch hỗ trợ C++ 11 trở lên. Đoạn code dưới đây được test với trình biên dịch Visual Studio 2017

Bỏ dấu tiếng Việt trong C++

std::wstring RemoveVietnameseTone(const std::wstring& text) {
  std::wstring result(text);
  result = std::regex_replace(result, std::wregex(L"à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ|/g"), L"a");
  result = std::regex_replace(result, std::wregex(L"À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ|/g"), L"A");
  result = std::regex_replace(result, std::wregex(L"è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ|/g"), L"e");
  result = std::regex_replace(result, std::wregex(L"È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ|/g"), L"E");
  result = std::regex_replace(result, std::wregex(L"ì|í|ị|ỉ|ĩ|/g"), L"i");
  result = std::regex_replace(result, std::wregex(L"Ì|Í|Ị|Ỉ|Ĩ|/g"), L"I");
  result = std::regex_replace(result, std::wregex(L"ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ|/g"), L"o");
  result = std::regex_replace(result, std::wregex(L"Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ|/g"), L"O");
  result = std::regex_replace(result, std::wregex(L"ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ|/g"), L"u");
  result = std::regex_replace(result, std::wregex(L"Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ|/g"), L"U");
  result = std::regex_replace(result, std::wregex(L"ỳ|ý|ỵ|ỷ|ỹ|/g"), L"y");
  result = std::regex_replace(result, std::wregex(L"Ỳ|Ý|Ỵ|Ỷ|Ỹ|/g"), L"y");
  result = std::regex_replace(result, std::wregex(L"đ"), L"d");
  result = std::regex_replace(result, std::wregex(L"Đ"), L"D");
  return result;
}

Download:
remove_vietnamese_tone_main.cc

Hy vọng sẽ giúp ích được cho nhiều người.

Nếu đoạn mã trên có bug hoặc có thể cải thiện, hãy comment xuống dưới giúp mình, xin cảm ơn.

8 thoughts on “Bỏ dấu tiếng việt trong C++

  1. Premiumzug

    from lat. manus – “hand” and scribo – “I write”) ]

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *