Linux/C 检查字符是否包含空格、换行符或制表符

发布于 2024-09-07 09:41:55 字数 119 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓬勃野心 2024-09-14 09:41:55

看一下字符分类例程:man isspace

Take a look at character classification routines: man isspace.

晨敛清荷 2024-09-14 09:41:55

创建一个包含感兴趣字符的字符数组。然后使用 strchr() 搜索字符串中是否存在该字符。

char charSet[] = { ' ', '\n', '\t', 0 };
char c;

// code that puts a character in c

if (strchr(charSet, c) != NULL)
{
    // it is one of the set
}

Create a char array containing the characters of interest. Then use strchr() to search for the presence of the char in the string.

char charSet[] = { ' ', '\n', '\t', 0 };
char c;

// code that puts a character in c

if (strchr(charSet, c) != NULL)
{
    // it is one of the set
}
风尘浪孓 2024-09-14 09:41:55

您正在寻找的函数是strpbrk()

#include <stdio.h>
#include <string.h>

int check_whitespace (char *str)
{
  char key[] = { ' ', '\n', '\t', 0 };
  return strpbrk (str, key);
}

The function you are looking for is strpbrk().

#include <stdio.h>
#include <string.h>

int check_whitespace (char *str)
{
  char key[] = { ' ', '\n', '\t', 0 };
  return strpbrk (str, key);
}
绿萝 2024-09-14 09:41:55

让我们假设您的意思是,将输入到 GtkEntry 中的内容添加到 char 数组(C 术语中的字符串,前提是它以 null 结尾)。然后检查该 char 数组是否至少包含一个或多个“空格”字符(根据区域设置,因此我们使用 isspace),

char *array;
int i;
//..
bool contains_space = false;
for(i = 0; i < strlen(array); i++) {
  if ( isspace(array[i]) ) {
    contains_space = true;
    break;
  }
}
// return contains_space

例如可以将其转换为函数。

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),

char *array;
int i;
//..
bool contains_space = false;
for(i = 0; i < strlen(array); i++) {
  if ( isspace(array[i]) ) {
    contains_space = true;
    break;
  }
}
// return contains_space

which can be turned into a function for example.

一片旧的回忆 2024-09-14 09:41:55

您可能会考虑使用如下函数,该函数计算给定字符串中空白字符的数量,如果找到任何字符(即 TRUE),则给出正整数;如果没有找到,则返回 0(即 FALSE);如果出错,则返回 -1。

#include <ctype.h>
static int
ws_count(char *s)
{
    int n = -1;
    if (s != NULL) {
        char *p;
        for (n = 0, p = s; *p != '\0'; p++) {
            if (isspace(*p)) {
                n++;
            }
        }
    }
    return n;
}

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.

#include <ctype.h>
static int
ws_count(char *s)
{
    int n = -1;
    if (s != NULL) {
        char *p;
        for (n = 0, p = s; *p != '\0'; p++) {
            if (isspace(*p)) {
                n++;
            }
        }
    }
    return n;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文