C语言 是否可以只打印出 C 字符串的某个部分,而不创建单独的子字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7780809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to print out only a certain section of a C-string, without making a separate substring?
提问by Tim
Say I have the following:
说我有以下几点:
char* string = "Hello, how are you?";
Is it possible to print out only the last 5 bytes of this string? What about the first 5 bytes only? Is there some variation of printfthat would allow for this?
是否可以仅打印出此字符串的最后 5 个字节?仅前 5 个字节怎么样?是否有一些变化printf允许这样做?
回答by Rob?
Is it possible to print out only the last 5 bytes of this string?
是否可以仅打印出此字符串的最后 5 个字节?
Yes, just pass a pointer to the fifth-to-the-last character. You can determine this by string + strlen(string) - 5.
是的,只需传递一个指向倒数第五个字符的指针。您可以通过 确定这一点string + strlen(string) - 5。
What about the first 5 bytes only?
仅前 5 个字节怎么样?
Use a precision specifier: %.5s
使用精度说明符: %.5s
#include <stdio.h>
#include <string.h>
char* string = "Hello, how are you?";
int main() {
/* print at most the first five characters (safe to use on short strings) */
printf("(%.5s)\n", string);
/* print last five characters (dangerous on short strings) */
printf("(%s)\n", string + strlen(string) - 5);
int n = 3;
/* print at most first three characters (safe) */
printf("(%.*s)\n", n, string);
/* print last three characters (dangerous on short strings) */
printf("(%s)\n", string + strlen(string) - n);
return 0;
}
回答by paxdiablo
Yes, the last five bytes of that string can be done with:
是的,该字符串的最后五个字节可以通过以下方式完成:
printf ("%s\n", &(string[strlen (string) - 5]));
The first five can be done with:
前五个可以通过以下方式完成:
printf ("%.5s\n", string);
You can combine the two to get substrings within the string as well. The word howcan be printed with:
您也可以将两者结合起来以获取字符串中的子字符串。这个词how可以打印为:
printf ("%.3s\n", &(string[strlen (string) + 7]));
You dohave to be careful that the string is long enough for this to work. Printing the last five characters of a one-character string will cause undefined behaviour since the index ends up at -4. In other words, check the string length before attempting this.
你千万要小心,字符串足够长,这个工作。打印一个单字符字符串的最后五个字符将导致未定义的行为,因为索引结束于-4。换句话说,在尝试之前检查字符串长度。
回答by Kapil Vyas
Two solutions:
两种解决方案:
Say given a predicatable string with same length - I will use date as an example and asked to split into HH:MM:SS.DDDDDDD
假设给定一个长度相同的可预测字符串 - 我将使用日期作为示例并要求拆分为 HH:MM:SS.DDDDDDD
char date[14] = "2359591234567";
[1] Readable Implementation:
[1] 可读实现:
char hh[3] = {0};
char mm[3] = {0};
char ss[3] = {0};
char dec[8] = {0};
strncpy ( hh, date, 2 );
strncpy ( mm, date+2, 2 );
strncpy ( ss, date+4, 2 );
strncpy ( dec, date+6, 7 );
printf("%s:%s:%s.%s\n", hh, mm, ss, dec);
[2] Short Implementation:
[2] 简短实现:
Either:
任何一个:
printf("%.2s:%.2s:%.2s.%.7s\n", date, date+2, date+4, date+6);
or:
或者:
printf("%2.2s:%2.2s:%2.2s.%7.7s\n", date, date+2, date+4, date+6);
Should work.
应该管用。
Instead of printf - you can use sprintf and copy to a buffer. I would also check for the correct length to avoid unpredictable behavior.
而不是 printf - 您可以使用 sprintf 并复制到缓冲区。我还会检查正确的长度以避免不可预测的行为。
In either case - the output will be:
在任何一种情况下 - 输出将是:
23:59:59.1234567

