Linux/C 检查字符是否包含空格、换行符或制表符
我有一个 GtkEntry,用户必须在其中输入 IP 号或主机名。当按下按钮时,用户在条目中键入的内容将添加到字符中。如何以编程方式检查该字符是否包含空格、换行符或制表符?我不需要删除它们,只是想知道它们是否存在。提前致谢!
I have a GtkEntry where the user has to enter an IP number or a hostname. When the button is pressed what the user typed into the entry is added to a char. How can I programmatically check if this char contains spaces, the newline character or the tab character? I don't need to remove them, just to know if they exist. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看一下字符分类例程:
man isspace
。Take a look at character classification routines:
man isspace
.创建一个包含感兴趣字符的字符数组。然后使用 strchr() 搜索字符串中是否存在该字符。
Create a char array containing the characters of interest. Then use strchr() to search for the presence of the char in the string.
您正在寻找的函数是strpbrk()。
The function you are looking for is strpbrk().
让我们假设您的意思是,将输入到 GtkEntry 中的内容添加到 char 数组(C 术语中的字符串,前提是它以 null 结尾)。然后检查该 char 数组是否至少包含一个或多个“空格”字符(根据区域设置,因此我们使用 isspace),
例如可以将其转换为函数。
Let us suppose you mean that what is typed into the GtkEntry is added to an array of char (a string, in C terminology, provided that it is null terminated). Then to check if that array of char contains at least one or more of "space" characters (according to the locale, so we use isspace),
which can be turned into a function for example.
您可能会考虑使用如下函数,该函数计算给定字符串中空白字符的数量,如果找到任何字符(即 TRUE),则给出正整数;如果没有找到,则返回 0(即 FALSE);如果出错,则返回 -1。
You might consider a function such as the following which counts the number of whitespace characters in the given string giving a positive integer is any are found (i.e. TRUE), zero if none are found (i.e. FALSE) and -1 on error.