当前位置:   article > 正文

判断一个字符中是否有乱码

c# 判断 中文乱码
  1. package com.test;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. /**
  5. * @description 判断中文是否乱码
  6. */
  7. public class MessyCodeCheck {
  8. public static boolean isChinese(char c) {
  9. Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
  10. if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
  11. || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
  12. || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
  13. || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
  14. || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
  15. || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
  16. return true;
  17. }
  18. return false;
  19. }
  20. public static boolean isMessyCode(String strName) {
  21. Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");
  22. Matcher m = p.matcher(strName);
  23. String after = m.replaceAll("");
  24. String temp = after.replaceAll("\\p{P}", "");
  25. char[] ch = temp.trim().toCharArray();
  26. float chLength = ch.length;
  27. float count = 0;
  28. for (int i = 0; i < ch.length; i++) {
  29. char c = ch[i];
  30. if (!Character.isLetterOrDigit(c)) {
  31. if (!isChinese(c)) {
  32. count = count + 1;
  33. }
  34. }
  35. }
  36. float result = count / chLength;
  37. if (result > 0.2) {//乱码字符 大于原内容的20%算是乱码
  38. return true;
  39. } else {
  40. return false;
  41. }
  42. }
  43. public static void main(String[] args) {
  44. System.out.println(isMessyCode("XYZr�������ABCDԴ"));
  45. System.out.println(isMessyCode("平台新¥%&#@*(版本发布"));
  46. System.out.println(isMessyCode("System.out.println"));
  47. }
  48. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/372292
推荐阅读
相关标签
  

闽ICP备14008679号