如何计算文件中的所有字符? [英] How can I count all characters in a file?

查看:100
本文介绍了如何计算文件中的所有字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我需要知道如何计算文件中的所有字符...


谢谢你的帮助.
Painjofshem;)

Hey guys

I need to know how I can count all characters in a file...


Thanks for your help.
Painjofshem ;)

推荐答案

如果文本文件是ASCII(或ANSI)编码的,则字符数严格等于文件的长度.

如果文件以Unicode编码,则字符数取决于字符,即使对于UTF-16也是如此,因为某些字符可能是两个16位字(代理对),所以您确实需要计算字符.以一个(Unicode)字符串读取所有文件并返回其长度.

仅当编码为UTF-32时,字符数才由文件的长度定义.您需要先查看是否有BOM.字符数为fileSize/4 - BOM,其中BOM = 0 or 1.

—SA
If the file the text file is ASCII (or ANSI) encoded, number of characters is strictly equal to the length of the file.

If the file is encoded in Unicode, the number of character depends on characters, even for UTF-16, because some characters could be in two 16-bit words (surrogate pair), so you will really need to count characters. Read all file in one (Unicode) string and return its length, that''s it.

Only if encoding is UTF-32, number of characters is defined by length of the file. You need to see if there is BOM at the first work or not. The number of characters is fileSize/4 - BOM, where BOM = 0 or 1.

—SA


尝试
Java字数统计-Java中的字数示例 [
Try
Java Word Count - Word Count Example in Java[^]


这篇关于如何计算文件中的所有字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆