在 C 中打印 char[] 的一部分的最简单方法

发布于 2024-07-08 10:11:39 字数 150 浏览 9 评论 0原文

假设我有一个 char* str = "0123456789",我想剪切第一个和最后三个字母并只打印中间,最简单、最安全的方法是什么?

现在的技巧是:要剪切的部分和要打印的部分的大小是可变的,因此我可以有一个很长的 char* ,也可以有一个非常小的 char* 。

Let's say I have a char* str = "0123456789" and I want to cut the first and the last three letters and print just the middle, what is the simplest, and safest, way of doing it?

Now the trick: The portion to cut and the portion to print are of variable size, so I could have a very long char*, or a very small one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

聽兲甴掵 2024-07-15 10:11:39

您可以使用 printf() 和特殊格式字符串:

char *str = "0123456789";
printf("%.6s\n", str + 1);

%s 转换说明符中的精度指定要打印的最大字符数。 您也可以使用变量来指定运行时的精度:

int length = 6;
char *str = "0123456789";    
printf("%.*s\n", length, str + 1);

在此示例中,* 用于指示下一个参数 (length) 将包含 %s< /code> 转换时,对应的参数必须是 int

指针算术可用于指定起始位置,就像我上面所做的那样。

[编辑]

还有一点,如果您的字符串比精度说明符短,则将打印更少的字符,例如:

int length = 10;
char *str = "0123456789";
printf("%.*s\n", length, str + 5);

将打印“56789”。 如果您总是想要打印一定数量的字符,请指定最小字段宽度和精度:

printf("%10.10s\n", str + 5);

printf("%*.*s\n", length, length, str + 5);

将打印:

"     56789"

您可以使用减号左对齐字段中的输出:

printf("%-10.10s\n", str + 5);

最后,最小字段宽度和精度精度可以不同,即

printf("%8.5s\n", str);

在 8 个字符字段中最多打印 5 个右对齐的字符。

You can use printf(), and a special format string:

char *str = "0123456789";
printf("%.6s\n", str + 1);

The precision in the %s conversion specifier specifies the maximum number of characters to print. You can use a variable to specify the precision at runtime as well:

int length = 6;
char *str = "0123456789";    
printf("%.*s\n", length, str + 1);

In this example, the * is used to indicate that the next argument (length) will contain the precision for the %s conversion, the corresponding argument must be an int.

Pointer arithmetic can be used to specify the starting position as I did above.

[EDIT]

One more point, if your string is shorter than your precision specifier, less characters will be printed, for example:

int length = 10;
char *str = "0123456789";
printf("%.*s\n", length, str + 5);

Will print "56789". If you always want to print a certain number of characters, specify both a minimum field width and a precision:

printf("%10.10s\n", str + 5);

or

printf("%*.*s\n", length, length, str + 5);

which will print:

"     56789"

You can use the minus sign to left-justify the output in the field:

printf("%-10.10s\n", str + 5);

Finally, the minimum field width and the precision can be different, i.e.

printf("%8.5s\n", str);

will print at most 5 characters right-justified in an 8 character field.

葮薆情 2024-07-15 10:11:39

罗伯特·甘布尔和史蒂夫分别拥有大部分作品。
组装成一个整体:

void print_substring(const char *str, int skip, int tail)
{
    int len = strlen(str);
    assert(skip >= 0);
    assert(tail >= 0 && tail < len);
    assert(len > skip + tail);
    printf("%.*s", len - skip - tail, str + skip);
}

调用示例:

print_substring("0123456789", 1, 3);

Robert Gamble and Steve separately have most of the pieces.
Assembled into a whole:

void print_substring(const char *str, int skip, int tail)
{
    int len = strlen(str);
    assert(skip >= 0);
    assert(tail >= 0 && tail < len);
    assert(len > skip + tail);
    printf("%.*s", len - skip - tail, str + skip);
}

Invocation for the example:

print_substring("0123456789", 1, 3);
寂寞花火° 2024-07-15 10:11:39

如果您不介意修改数据,您可以只进行一些指针运算。 假设 str 是一个 char 指针而不是一个数组:

char string[] = "0123456789";
char *str = string;

str += 3; // "removes" the first 3 items
str[4] = '\0'; // sets the 5th item to NULL, effectively truncating the string

printf(str); // prints "3456"

If you don't mind modifying the data, you could just do some pointer arithmetic. This is assuming that str is a char pointer and not an array:

char string[] = "0123456789";
char *str = string;

str += 3; // "removes" the first 3 items
str[4] = '\0'; // sets the 5th item to NULL, effectively truncating the string

printf(str); // prints "3456"
打小就很酷 2024-07-15 10:11:39

这是我从我的个人库中挖出来的一个干净而简单的子字符串函数,可能有用:

char *
substr(const char *src, size_t start, size_t len)
{
  char *dest = malloc(len+1);
  if (dest) {
    memcpy(dest, src+start, len);
    dest[len] = '\0';
  }
  return dest;
}

它可能是不言自明的,但它需要一个字符串、一个起始位置(从零开始)和一个长度,并返回原始的子字符串如果 malloc 失败,则为字符串或空指针。 当不再需要内存时,调用者可以释放返回的指针。 按照 C 的精神,该函数不会验证所提供的起始位置和长度。

Here is a clean and simple substring function I dug up from my personal library that may be useful:

char *
substr(const char *src, size_t start, size_t len)
{
  char *dest = malloc(len+1);
  if (dest) {
    memcpy(dest, src+start, len);
    dest[len] = '\0';
  }
  return dest;
}

It's probably self-explanatory but it takes a string, a starting position (starting at zero), and a length and returns a substring of the original string or a null pointer if malloc fails. The pointer returned can be free'd by the caller when the memory is no longer needed. In the spirit of C, the function doesn't validate the starting position and length provided.

枫林﹌晚霞¤ 2024-07-15 10:11:39

我相信你可以使用 printf 实现一些魔法,它只会打印一定数量的字符,但它并不被普遍理解或使用。 我们在之前的工作中尝试过这样做,但无法使其始终如一地工作。

我要做的就是保存一个字符,将字符串中的该字符清空,打印它,然后将其保存回来。

I believe there is some magic you can do with printf that will only print a certain number of characters, but it's not commonly understood or used. We tried to do it at a previous job and couldn't get it to work consistently.

What I would do is save off a character, null that character in the string, print it, then save it back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文