获得字符数组的一部分 [英] Get part of a char array

查看:124
本文介绍了获得字符数组的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这是一个非常愚蠢的问题,但我似乎无法在别处找到答案!

I feel like this is a really silly question, but I can't seem to find an answer anywhere!

是否有可能从一个字符数组得到一组字符的?扔了一些伪code:

Is it possible to get a group of chars from a char array? to throw down some pseudo-code:

char arry[20] = "hello world!";
char part[10] = arry[0-4];
printf(part);

输出:

hello

所以,我可以从像这样的字符数组的一段不循环,让他们炭由-char或转换为字符串,所以我可以用SUBSTR()?

So, can I get a segment of chars from an array like this without looping and getting them char-by-char or converting to strings so I can use substr()?

推荐答案

在短,没有。 C风格的弦根本就没有这样的。你将不得不使用手动循环,或函数strncpy(),或通过C做++ 的std :: string的功能。鉴于你在C ++中,你不妨做一切与C ++字符串!

In short, no. C-style "strings" simply don't work that way. You will either have to use a manual loop, or strncpy(), or do it via C++ std::string functionality. Given that you're in C++, you may as well do everything with C++ strings!

侧面说明

碰巧的是,为您的特定示例应用程序,您可以通过按的printf提供的功能,只是实现这个()

As it happens, for your particular example application, you can achieve this simply via the functionality offered by printf():

printf("%.5s\n", arry);

这篇关于获得字符数组的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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