Count number of characters: Difference between revisions

mNo edit summary
Tag: wikieditor
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
Counting number of characters (or bytes) in different approaches
Counting number of characters (or bytes) in different approaches


== Character Count vs. Byte Count Comparison ==
<table border="1" class="wikitable sortable">
<table border="1" class="wikitable sortable">
<tr>
<tr>
Line 35: Line 36:
<td>敏捷的棕毛狐狸從懶狗身上躍過</td>
<td>敏捷的棕毛狐狸從懶狗身上躍過</td>
<td>14</td>
<td>14</td>
<td>28</td>
<td>42</td>
</tr>
</tr>
</table>
</table>
: [[Image:Owl icon.jpg]] Each Chinese character is approximately 3 bytes in UTF-8
Common Chinese characters (CJK Unified Ideographs, U+4E00 ~ U+9FFF) are all 3 bytes — for example, "你", "好", "狐", and "象". Characters beyond U+FFFF (outside the Basic Multilingual Plane, BMP<ref>[https://en.wikipedia.org/wiki/Plane_(Unicode) Plane (Unicode) - Wikipedia]</ref>) take 4 bytes. These are mostly rare characters from CJK Extension B (U+20000 and up)<ref>[https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_Extension_B CJK Unified Ideographs Extension B - Wikipedia]</ref>, such as "[https://www.cns11643.gov.tw/wordView.jsp?ID=993142 𤆬]" (U+241AC) and "[https://www.cns11643.gov.tw/wordView.jsp?ID=402472 𠮷]" (U+20BB7).


== How to count characters with PHP ==
== How to count characters with PHP ==
Line 98: Line 103:
== How to count characters with BASH ==
== How to count characters with BASH ==
Step1: Using [https://www.computerhope.com/unix/uwc.htm Linux wc command]
Step1: Using [https://www.computerhope.com/unix/uwc.htm Linux wc command]
<pre>
# Count the total number of characters in a file named "input.txt", while ignoring all whitespace characters (including spaces, tabs, newlines, etc.).
tr -d '\r\n[:space:]' < input.txt | wc -m
</pre>
<pre>
<pre>
# print the character counts of txt files (contains the count of return symbol)
# print the character counts of txt files (contains the count of return symbol)
Line 115: Line 126:


Number of characters (not contains the [[Return symbol | return symbol]]) = result of {{kbd | key=<nowiki>wc -m *.txt</nowiki>}} - result of {{kbd | key=<nowiki>wc -m *.txt</nowiki>}} * 2 - 1 (the last blank line costs 1 character) - number of the whitespaces
Number of characters (not contains the [[Return symbol | return symbol]]) = result of {{kbd | key=<nowiki>wc -m *.txt</nowiki>}} - result of {{kbd | key=<nowiki>wc -m *.txt</nowiki>}} * 2 - 1 (the last blank line costs 1 character) - number of the whitespaces


== How to count characters with Python ==
== How to count characters with Python ==
Line 165: Line 175:
* [https://www.ithome.com.tw/voice/131688 Unicode與JavaScript字串 | iThome]
* [https://www.ithome.com.tw/voice/131688 Unicode與JavaScript字串 | iThome]


[[Category:Software]] [[Category:Programming]] [[Category:Data Science]] [[Category:String manipulation]] [[Category:PHP]] [[Category:MySQL]]
[[Category: Software]]  
[[Category: Programming]]  
[[Category: Data Science]]  
[[Category: String manipulation]]  
[[Category: PHP]]  
[[Category: MySQL]]
[[Category: Revised with LLMs]]