Count the number of occurrences of arrays, Chinese, English letters, spaces, and other special characters in a text

Source: Internet
Author: User

  1. Package util;
  2. Public class Countstr {
  3. /** 
  4. * There is a string that contains Chinese characters, English characters and numbers, please count and print out the number of individual characters
  5. * SMS sending platform, text message Word Count control Query method
  6. */
  7. public static void Main (string[] args) {
  8. //string str = "adasf AAADFD I am Chinese," 123 ";
  9. //string str = "Gold vest high-end commodity trading platform--2013 whole city Love carat diamond project pre-sale, 18 months, 30,000 start, annual 8%, pre-order please call Exhibition Heng Private Wealth: 18611297979";
  10. String str = "Zhan Heng Finance, established in Beijing in 2004, is one of the largest financial advisory institutions in China. obtained the National Independent Fund sale license. is one of the 10 core sponsors of the 2013 China Open tennis tournament. The company has been established for more than 10 years, in order to provide customers with a comprehensive family financial planning has accumulated a very rich experience. Currently has a high-end loyal customers more than 10,000, the allocation of customer funds more than 20 billion yuan, ranked in the top three industries.  ";
  11. System.out.println ("[Total number of characters 1]:" +countsum (str));
  12. System.out.println ("--------------------");
  13. System.out.println ("[Total number of characters 2]:" +countsum2 (str));
  14. System.out.println ("--------------------");
  15. System.out.println ("[Total number of characters 3]:" +str.length ());
  16. }
  17. public static int countsum (String str) {
  18. int unicodecount = 0;
  19. int szcount = 0;
  20. int zmcount = 0;
  21. For (int i = 0; i < str.length (); i++) {
  22. char c = str.charat (i);
  23. if (c >= ' 0 ' && C <= ' 9 ') {
  24. szcount++;
  25. }Else if ((C >= ' a ' && c<=' z ') | | (c >= ' A ' && c<=' Z ')) {
  26. zmcount++;
  27. }else{
  28. unicodecount++;
  29. }
  30. }
  31. System.out.println ("Unicode:" +unicodecount);
  32. System.out.println ("number:" +szcount);
  33. System.out.println ("Letter:" +zmcount);
  34. int sum=szcount+zmcount+unicodecount;
  35. return sum;
  36. }
  37. public static int countSum2 (String str) {
  38. int abccount = 0;
  39. int numcount = 0;
  40. int spacecount = 0;
  41. int othercount = 0;
  42. char[] B = Str.tochararray ();
  43. For (int i = 0; i < b.length; i++) {
  44. if (b[i]>=' a ' &&b[i]<=' z ' | | b[i]>=' A ' &&b[i]<=' Z ') {
  45. abccount++;
  46. }Else if (b[i]>=' 0 ' &&b[i]<=' 9 ') {
  47. numcount++;
  48. }Else if (b[i]==') {
  49. spacecount++;
  50. }else{
  51. othercount++;
  52. }
  53. }
  54. int sum=abccount+numcount+spacecount+othercount;
  55. System.out.println ("The string contains the number of letters in English:" + abccount);
  56. System.out.println ("The number ofdigits contained in the string is:" + numcount);
  57. System.out.println ("The number of spaces contained in the string is:" + spacecount);
  58. System.out.println ("Other characters contained in the string are:" + othercount);
  59. return sum;
  60. }
  61. }

Count the number of occurrences of arrays, Chinese, English letters, spaces, and other special characters in a text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.