在C字符串,如何让子 [英] Strings in c, how to get subString

查看:77
本文介绍了在C字符串,如何让子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty新的C

我有一个字符串:

 的char * someString;

如果我想这个字符串的第一个5个字母,并希望将它设置为 otherString ,我会怎么做呢?


解决方案

 的#include<&string.h中GT;
...
炭otherString [6]; //注6,而不是5,有一个那里的空终止
...
函数strncpy(otherString,someString,5);
otherString [5] ='\\ 0'; //放置空终止

I am pretty new to C.

I have a string:

char * someString;

If I want the first 5 letters of this string and want to set it to otherString, how would I do it?

解决方案

#include <string.h>
...
char otherString[6]; // note 6, not 5, there's one there for the null terminator
...
strncpy(otherString, someString, 5);
otherString[5] = '\0'; // place the null terminator

这篇关于在C字符串,如何让子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆