开发者

How to detect a tab in a text file?

开发者 https://www.devze.com 2023-03-11 05:58 出处:网络
Is detecting tabs same as detecting the spaces? i.e. for detecting a space, I would jus开发者_运维知识库t compare the space character with its ascii number.

Is detecting tabs same as detecting the spaces? i.e. for detecting a space, I would jus开发者_运维知识库t compare the space character with its ascii number.

For a tab do I have to search for '\t' character in the file or there is some other way?


if('\t' == myChar)

This would work, and would be better than checking against 9 since 9 may not be a guaranteed value across all architectures.


Assuming you are working with ASCII data, you can just search for a byte with value '\t' (9) in the text file. Tabs are represented as a single byte in text files and most libraries for reading files don't do anything special with those bytes.


A tab is just another character so you can check for the ASCII value if you want.


Although a tab appears as 4 or 8 spaces in an editor, it is actually represented as a single character ('\t', like you mentioned) inside a file. Both the space character and the tab character take up one byte. So basically, u are correct in your assumption.

0

精彩评论

暂无评论...
验证码 换一张
取 消