C语言 获取字符串的前 10 个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16348512/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:14:49  来源:igfitidea点击:

Getting the first 10 characters of a string?

cstring

提问by user2341069

I have not been able to find any information with a web-search. Where should I be looking?

我无法通过网络搜索找到任何信息。我应该去哪里找?

回答by Lefteris E

char myString[256]; // Input string
char dest[256];     // Destination string

strncpy(dest, myString, 10);
dest[10] = 0; // null terminate destination

回答by tianz

char source[] = "abcdefthijklmn";
char target[100];

strncpy(target, source, 10);
target[10] = '
char* someString = "your string goes here";

int main() 
{
  int n = 10;
  printf("(%.*s)\n", n, someString);

  return 0;
}
'; // IMPORTANT!

回答by Ashish Srivastava

Adding to the above answers:

补充上面的答案:

char source[] = "abcdefthijklmn";
char target[100];

snprintf(target, sizeof target, "%.10s", source);

回答by chqrlie

There are many different ways to achieve your goal:

有许多不同的方法可以实现您的目标:

  • You can use snprintf(safest):

    char source[] = "abcdefthijklmn";
    char target[100];
    
    *target = '
    char source[] = "abcdefthijklmn";
    char target[100];
    size_t len = strlen(source);
    
    if (len > 10)
        len = 10;
    memcpy(target, source, len);
    target[len] = '
    char source[] = "abcdefthijklmn";
    char target[100];
    size_t i;
    for (i = 0; i < 10; i++) {
        target[i] = source[i];
    }
    target[i] = '
    char source[] = "abcdefthijklmn";
    char target[100];
    
    snprintf(target, sizeof target, "%.10s", source);
    
    ';
    ';
    '; strncat(target, source, 10);
  • You can use strncatif you know the destination has at least 11 elements:

    char source[] = "abcdefthijklmn";
    char target[100];
    
    *target = '
    char source[] = "abcdefthijklmn";
    char target[100];
    size_t len = strlen(source);
    
    if (len > 10)
        len = 10;
    memcpy(target, source, len);
    target[len] = '
    char source[] = "abcdefthijklmn";
    char target[100];
    size_t i;
    for (i = 0; i < 10; i++) {
        target[i] = source[i];
    }
    target[i] = '
       # include <stdio.h>
       # include <string.h>
       //Strings. String lenght terminator.
       //KHO2016.no1. mingw (TDM-GCC-32) . c-ansi .
       int main ()
       {
       //declare
       char src_str[20],dst_str[10];
    
       //valuate
       printf ("Enter a sentance of 20 letters\n");
       gets (src_str);
       strcpy (dst_str,src_str);
    
       //calculate
       dst_str [10] ='
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {   
        char source[] = "abcdefghijklmnopqrstuvwxyz";
        char dest[11];
        memset(dest, '##代码##', sizeof(dest));
    
        sprintf(dest, "%.10s", source);
    
        printf("%s", dest); // abcdefghij
        return 0;
    }
    
    '; // from the "www.stack overflow" printf ("%s",dst_str); printf ("\n"); //terminate return 0; }
    ';
    ';
    '; strncat(target, source, 10);
  • You can use strlenand memcpy(same assumption about the size of destination):

    ##代码##
  • You can use a loop (same assumption about the size of destination):

    ##代码##
  • You could but should notuse strncpy()

  • 您可以使用snprintf(最安全):

    ##代码##
  • strncat如果您知道目的地至少有 11 个元素,则可以使用:

    ##代码##
  • 您可以使用strlenmemcpy(关于目的地大小的相同假设):

    ##代码##
  • 您可以使用循环(对目的地大小的相同假设):

    ##代码##
  • 你可以但不应该使用strncpy()

回答by TonyArra

If you're looking for a good source, here's an example of an online man page you can use: http://linux.die.net/man/3/strncpy

如果您正在寻找一个好的来源,这里有一个您可以使用的在线手册页示例:http: //linux.die.net/man/3/strncpy

Important to note: you could also use memcpy instead of strncpy, but it requires you to add your own null-terminating byte.

需要注意的重要事项:您也可以使用 memcpy 代替 strncpy,但它要求您添加自己的空终止字节。

Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.

警告:如果 src 的前 n 个字节中没有空字节,则放置在 dest 中的字符串不会以空值结尾。

Hence, memcpy and strncpy work almost the same here, and memcpy is more efficient and less prone to error.

因此,memcpy 和 strncpy 在这里的工作方式几乎相同,并且 memcpy 更高效且不易出错。

回答by Radar Blue

I got it to work like this.

我让它像这样工作。

##代码##

回答by w.b

You can also use sprintfwith .10precision format:

您还可以使用sprintf.10精度格式:

##代码##