SoFunction
Updated on 2025-03-06

C# method to count strings

This article shares the specific code of C# word count statistics (letters, numbers, Chinese characters, symbols) for your reference. The specific content is as follows

namespace test1
 
{
 public partial class Form1 : Form
 {
 public Form1()
 {
  InitializeComponent();
  this. = "gyou\rs\rsyou好吗d dsDDDDDDDDddssssss、‘\\Qiqiao';‘l;''''sssssssssssssssssssssssssssssssssssssssssssssssssssssssssasdddddddddddddddddddd\tddddddddddddddddddddddddddddddddddddddddddddddddda";
 }
 
 bool skipSpace = true;
 
 //Window loading private void Form1_Load(object sender, EventArgs e)
 {
  ();
  int cnt1=0, cnt2=0, cnt3=0, cnt4=0;
  if (this. != null)
  {
  string sLine;
  string s = ;
  if (skipSpace)
  {
   {
   sLine = (" ?", "").Replace(" ", "").Replace("\t", "").Replace("\n", "").Replace("\r", "");
   cnt2 += getByteLength(sLine);
   cnt1 += getWordLength(sLine);
   cnt3 += getdigitalLength(sLine);
   cnt4 += getcharLength(sLine);
    = ();// Word count    = ();//Number of bytes (excluding "","\t","\n","\r"," ?")    = ();//Number of numbers    = ();//Number of letters    = (cnt2 - cnt1).ToString();//The number of Chinese characters    = (cnt2 - cnt3 - cnt4 - (cnt2 - cnt1) * 2).ToString();//Number of symbols   }
  }
  }
 }
 
 //Content change event private void richTextBox1_TextChanged(object sender, EventArgs e)
 {
  bool skipSpace = true;
  int cnt1=0, cnt2=0, cnt3=0, cnt4=0;
  if (this. != null)
  {
  string sLine;
  string s = ;
  if (skipSpace)
  {
   sLine = (" ?", "").Replace(" ", "").Replace("\t", "").Replace("\n", "").Replace("\r", "");
   cnt2 += getByteLength(sLine);
   cnt1 += getWordLength(sLine);
   cnt3 += getdigitalLength(sLine);
   cnt4 += getcharLength(sLine);
    = ();// Word count    = ();//Number of bytes (excluding "","\t","\n","\r"," ?")    = ();//Number of numbers    = ();//Number of letters    = (cnt2 - cnt1).ToString();//The number of Chinese characters    = (cnt2 - cnt3 - cnt4 - (cnt2 - cnt1)*2).ToString();//Number of symbols  }
  }
 }
 
  /// <summary>
 /// Return word count /// </summary>
 /// <param name="s"></param>
 /// <returns></returns>
 private int getWordLength(string s)
 {
  if (s != null)
  return ;
  else
  return 0;
 }
 
 /// <summary>
 /// Return the number of words (0~9) /// </summary>
 /// <param name="s"></param>
 /// <returns></returns>
 private int getdigitalLength(string s)
 {
  int lx = 0;
  char[] q = ();
  for (int i = 0; i < ; i++)
  {
  if ((int)q[i] >= 48 && (int)q[i] <= 57)
  {
   lx += 1;
  }
  }
  return lx;
 }
 
 /// <summary>
 /// Return the number of words in letters (A~Z-a~z) /// </summary>
 /// <param name="s"></param>
 /// <returns></returns>
 private int getcharLength(string s)
 {
  int lz = 0;
  char[] q = ().ToCharArray();//Convert uppercase letters to lowercase letters  for (int i = 0; i < ; i++)
  {
  if ((int)q[i] >= 97 && (int)q[i] <= 122)//Lowercase letters  {
   lz += 1;
  }
  }
  return lz;
 }
 
 /// <summary>
 /// Return the number of bytes /// </summary>
 /// <param name="s"></param>
 /// <returns></returns>
 private int getByteLength(string s)
 {
  int lh = 0;
  char[] q = ();
  for (int i = 0; i < ; i++)
  {
  if ((int)q[i] >= 0x4E00 && (int)q[i] <= 0x9FA5) // Chinese characters  {
   lh += 2;
  }
  else
  {
   lh += 1;
  }
  }
  return lh;
 }
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.