将字符数组转换为字符串 [英] Converting Character Array to String

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

问题描述

我不知道这是不是一个愚蠢的问题。


我想知道如何转换由
$生成的字符数组b $ b以前对字符串的操作,即如何在

结尾添加空字符?到目前为止我还没能做到。有没有字符串功能我可以使用
吗?


有人可以帮忙吗?


Charles L

解决方案

" Charles L" < TJ *** @ dodo.com.au>在消息中写道

新闻:3f ******** @ news.comindico.com.au ...

我不知道这是不是是不是一个愚蠢的问题。

我想知道如何将
a之前操作生成的字符数组转换为字符串,即如何在
结束?到目前为止我还没能做到。有没有我可以使用的字符串功能?

任何人都可以帮忙吗?

Charles L




什么是字符串?

你在谈论STL的字符串吗?


一个字符数组是一个字符串。


如果你的意思是那个字符数组不必被空终止并且

字符串必须是,那么你的问题的答案将是:

尝试确定角色阵列的大小,然后执行以下操作:

string [length] = 0;


问候

Elias


" Charles L" < TJ *** @ dodo.com.au>写道:

我不知道这是不是一个愚蠢的问题。

我想知道如何转换生成的字符数组从
上一个操作到一个字符串,即如何在
末尾添加一个空字符?到目前为止我还没能做到。是否有我可以使用的字符串函数?




如果您知道字符串应该有多长,那就很简单:

array [length] =''\'''可以解决问题。如果你不知道它应该是多长时间,那就没有可行的方法从阵列中找出

,因为超过所需长度的字符可以填写

任何东西,包括看似正常的文字。


Richard


< blockquote> Charles L< tj *** @ dodo.com.au>写道:

我想知道如何将从先前操作生成的字符数组转换为字符串,即如何在




你跟踪你有多少个角色?


如果你知道这一点,那么你想要做的就很容易。


long nCharacters; / *应该设置为你的字符数* /

/ *有* /


yourString [nCharacters] =''\''' ;


这假设''yourString''有空间存储空字符。

如果你不知道它确实存在,你将需要动态分配一个

字符串,这将是。


long nCharacters; / *应该设置为你的字符数* /

/ *有* /

char * newString = NULL;


newString = malloc(nCharacters + 1); / *应检查NULL * /

strncpy(newString,yourString,nCharacters);

newString [nCharacters] =''\ 0'';

如果您不知道有多少字符或无法确定它,

您想要做的事情是不可能的。

-

== Eric Gorr ========= http://www.ericgorr。 net ========= ICQ:9293199 ===

因此智能的考虑总是包括

的好处和伤害" - 孙子

==侮辱,就像暴力一样,是无能的最后避难所...... ===


I don''t know if this is a stupid quesiton or not.

I would like to know how to convert an array of characters generated from a
previous operation to a string ie how do I append a null character at the
end? I haven''t been able to do it so far. Is there a string function I can
use?

Can anyone help?

Charles L

解决方案

"Charles L" <tj***@dodo.com.au> wrote in message
news:3f********@news.comindico.com.au...

I don''t know if this is a stupid quesiton or not.

I would like to know how to convert an array of characters generated from a previous operation to a string ie how do I append a null character at the
end? I haven''t been able to do it so far. Is there a string function I can
use?

Can anyone help?

Charles L



What is a string?
Are you talking about STL''s string ?

an array of characters is a string.

If you mean that array of characters doesn''t have to be null terminated and
string has to be, then the answer to your question would be:
Try to determine a size out of your character array then do this:
string[length] = 0;

Regards
Elias


"Charles L" <tj***@dodo.com.au> wrote:

I don''t know if this is a stupid quesiton or not.

I would like to know how to convert an array of characters generated from a
previous operation to a string ie how do I append a null character at the
end? I haven''t been able to do it so far. Is there a string function I can
use?



If you know how long the string is supposed to be, it''s simple:
array[length]=''\0'' will do the trick. If you don''t know how long it''s
supposed to be, there''s no feasible way to find out from the array
alone, since the characters past the required length can be filled with
anything, including seemingly normal text.

Richard


Charles L <tj***@dodo.com.au> wrote:

I would like to know how to convert an array of characters generated from a
previous operation to a string ie how do I append a null character at the
end? I haven''t been able to do it so far. Is there a string function I can
use?



Do you track how many characters you have?

If you know this, what you want to do is easy.

long nCharacters; /*should be set to the number of characters you*/
/*have*/

yourString[nCharacters] = ''\0'';

This does assume ''yourString'' has the space to store the null character.
If you do not know that it does, you will need to dynamically allocate a
string that will.

long nCharacters; /*should be set to the number of characters you*/
/*have*/
char *newString = NULL;

newString = malloc( nCharacters + 1 ); /*should check for NULL*/
strncpy( newString, yourString, nCharacters );
newString[nCharacters] = ''\0'';
If you do not know how many characters you have or cannot determine it,
what you want to do is impossible.
--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===


这篇关于将字符数组转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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